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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
|
/*
* SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.impl.protocol.jabber;
import java.util.*;
import org.jivesoftware.smack.*;
import org.jivesoftware.smackx.*;
import org.jivesoftware.smackx.jingle.*;
import org.jivesoftware.smackx.jingle.media.*;
import org.jivesoftware.smackx.jingle.listeners.*;
import org.jivesoftware.smackx.jingle.nat.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.OperationSetBasicTelephony.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
import net.java.sip.communicator.impl.protocol.jabber.mediamgr.*;
import org.jivesoftware.smackx.packet.DiscoverInfo;
/**
* Implements all call management logic and exports basic telephony support by
* implementing OperationSetBasicTelephony.
*
* @author Symphorien Wanko
*/
public class OperationSetBasicTelephonyJabberImpl
extends AbstractOperationSetBasicTelephony
implements RegistrationStateChangeListener,
JingleMediaListener,
JingleTransportListener,
JingleSessionRequestListener,
CreatedJingleSessionListener,
JingleSessionStateListener,
JingleSessionListener
{
/**
* The logger used by this class
*/
private static final Logger logger
= Logger.getLogger(OperationSetBasicTelephonyJabberImpl.class);
/**
* A reference to the <tt>ProtocolProviderServiceJabberImpl</tt> instance
* that created us.
*/
private ProtocolProviderServiceJabberImpl protocolProvider = null;
/**
* Contains references for all currently active (non ended) calls.
*/
private ActiveCallsRepository activeCallsRepository
= new ActiveCallsRepository(this);
/**
* The manager we use to initiate, receive and ... manage jingle session.
*/
private JingleManager jingleManager = null;
/**
* The transport manager is used by the <tt>jingleManager</tt> to handle transport
* method.
*/
private JingleTransportManager transportManager = null;
/**
* The media manager is used by the <tt>jingleManager</tt> to handle media
* session.
*/
private JingleMediaManager mediaManager = null;
/**
* Creates a new instance.
*
* @param protocolProvider a reference to the
* <tt>ProtocolProviderServiceJabberImpl</tt> instance that created us.
*/
public OperationSetBasicTelephonyJabberImpl(
ProtocolProviderServiceJabberImpl protocolProvider)
{
this.protocolProvider = protocolProvider;
protocolProvider.addRegistrationStateChangeListener(this);
transportManager = new BasicTransportManager();
mediaManager = new JingleScMediaManager();
}
/**
* Implementation of method <tt>registrationStateChange</tt> from
* interface RegistrationStateChangeListener for setting up (or down)
* our <tt>JingleManager</tt> when an <tt>XMPPConnection</tt> is available
*
* @param evt the event received
*/
public void registrationStateChanged(RegistrationStateChangeEvent evt)
{
if ((evt.getNewState() == RegistrationState.REGISTERED))
{
transportManager = new ICETransportManager(
protocolProvider.getConnection(),
"stun.iptel.org", 3478);
jingleManager = new JingleManager(
protocolProvider.getConnection(),
transportManager,
mediaManager);
jingleManager.addCreationListener(this);
jingleManager.addJingleSessionRequestListener(this);
logger.info("Jingle : ON ");
}
else if ((evt.getNewState() == RegistrationState.UNREGISTERED))
{
if (jingleManager != null)
{
jingleManager.removeCreationListener(this);
jingleManager.removeJingleSessionRequestListener(this);
jingleManager = null;
logger.info("Jingle : OFF ");
}
}
}
/**
* Create a new call and invite the specified CallParticipant to it.
*
* @param callee the jabber address of the callee that we should invite to a
* new call.
* @return CallParticipant the CallParticipant that will represented by
* the specified uri. All following state change events will be
* delivered through that call participant. The Call that this
* participant is a member of could be retrieved from the
* CallParticipatn instance with the use of the corresponding method.
* @throws OperationFailedException with the corresponding code if we fail
* to create the call.
*/
public Call createCall(String callee)
throws OperationFailedException
{
return createOutgoingCall(callee);
}
/**
* Create a new call and invite the specified CallParticipant to it.
*
* @param callee the address of the callee that we should invite to a
* new call.
* @return CallParticipant the CallParticipant that will represented by
* the specified uri. All following state change events will be
* delivered through that call participant. The Call that this
* participant is a member of could be retrieved from the
* CallParticipatn instance with the use of the corresponding method.
* @throws OperationFailedException with the corresponding code if we fail
* to create the call.
*/
public Call createCall(Contact callee)
throws OperationFailedException
{
return createOutgoingCall(callee.getAddress());
}
/**
* Init and establish the specified call.
*
* @param calleeAddress the address of the callee that we'd like to connect
* with.
*
* @return CallParticipant the CallParticipant that represented by
* the specified uri. All following state change events will be
* delivered through that call participant. The Call that this
* participant is a member of could be retrieved from the
* CallParticipatn instance with the use of the corresponding method.
*
* @throws OperationFailedException with the corresponding code if we fail
* to create the call.
*/
private CallJabberImpl createOutgoingCall(String calleeAddress)
throws OperationFailedException
{
OutgoingJingleSession outJS;
logger.info("creating outgoing call...");
if (protocolProvider.getConnection() == null) {
throw new OperationFailedException(
"Failed to create OutgoingJingleSession.\n"
+ "we don't have a valid XMPPConnection."
, OperationFailedException.INTERNAL_ERROR);
}
// we determine on which resource the remote user is connected if the
// resource isn't already provided
String fullCalleeURI = null;
if (calleeAddress.indexOf('/') > 0)
{
fullCalleeURI = calleeAddress;
}
else
{
fullCalleeURI = protocolProvider.getConnection().
getRoster().getPresence(calleeAddress).getFrom();
}
if (fullCalleeURI.indexOf('/') < 0)
{
throw new OperationFailedException(
"Failed to create OutgoingJingleSession.\n"
+ "User " + calleeAddress + " is unknown to us."
, OperationFailedException.INTERNAL_ERROR);
}
try
{
// with discovered info, we can check if the remote clients
// supports telephony but not if he don't, because
// a non conforming client can supports a feature
// without advertising it. So we don't rely on it (for the moment)
DiscoverInfo di = ServiceDiscoveryManager
.getInstanceFor(protocolProvider.getConnection())
.discoverInfo(fullCalleeURI);
if (di.containsFeature("http://www.xmpp.org/extensions/xep-0166.html#ns"))
{
logger.info(fullCalleeURI + ": jingle supported ");
}
else
{
logger.info(calleeAddress + ": jingle not supported ??? ");
//
// throw new OperationFailedException(
// "Failed to create OutgoingJingleSession.\n"
// + fullCalleeURI + " do not supports jingle"
// , OperationFailedException.INTERNAL_ERROR);
}
}
catch (XMPPException ex)
{
logger.warn("could not retrieve info for " + fullCalleeURI, ex);
}
try
{
outJS = jingleManager.createOutgoingJingleSession(fullCalleeURI);
}
catch (XMPPException ex)
{
throw new OperationFailedException(
"Failed to create OutgoingJingleSession.\n"
+ "This is most probably a network connection error."
, OperationFailedException.INTERNAL_ERROR
, ex);
}
CallJabberImpl call = new CallJabberImpl(protocolProvider);
CallParticipantJabberImpl callParticipant =
new CallParticipantJabberImpl(calleeAddress, call);
callParticipant.setJingleSession(outJS);
callParticipant.setState(CallParticipantState.INITIATING_CALL);
fireCallEvent(CallEvent.CALL_INITIATED, call);
activeCallsRepository.addCall(call);
outJS.start();
return (CallJabberImpl) callParticipant.getCall();
}
/**
* Returns an iterator over all currently active calls.
*
* @return an iterator over all currently active calls.
*/
public Iterator getActiveCalls()
{
return activeCallsRepository.getActiveCalls();
}
/**
* Resumes communication with a call participant previously put on hold.
*
* @param participant the call participant to put on hold.
*/
public void putOffHold(CallParticipant participant)
{
/** @todo implement putOffHold() */
((CallParticipantJabberImpl) participant).getJingleSession().
getJingleMediaSession().setTrasmit(true);
}
/**
* Puts the specified CallParticipant "on hold".
*
* @param participant the participant that we'd like to put on hold.
*/
public void putOnHold(CallParticipant participant)
{
/** @todo implement putOnHold() */
((CallParticipantJabberImpl) participant).getJingleSession().
getJingleMediaSession().setTrasmit(false);
}
/**
* Implements method <tt>hangupCallParticipant</tt>
* from <tt>OperationSetBasicTelephony</tt>.
*
* @param participant the participant that we'd like to hang up on.
* @throws ClassCastException if participant is not an instance of
* CallParticipantJabberImpl.
*
* @throws OperationFailedException if we fail to terminate the call.
*
* // TODO: ask for suppression of OperationFailedException from the interface.
* // what happens if hangup fails ? are we forced to continue to talk ? :o)
*/
public void hangupCallParticipant(CallParticipant participant)
throws ClassCastException, OperationFailedException
{
CallParticipantJabberImpl callParticipant
= (CallParticipantJabberImpl)participant;
try
{
callParticipant.getJingleSession().terminate();
}
catch (XMPPException ex)
{
ex.printStackTrace();
}
finally
{
callParticipant.setState(CallParticipantState.DISCONNECTED);
}
}
/**
* Implements method <tt>answerCallParticipant</tt>
* from <tt>OperationSetBasicTelephony</tt>.
*
* @param participant the call participant that we want to answer
* @throws OperationFailedException if we fails to answer
*/
public void answerCallParticipant(CallParticipant participant)
throws OperationFailedException
{
CallParticipantJabberImpl callParticipant
= (CallParticipantJabberImpl)participant;
try
{
((IncomingJingleSession)callParticipant.getJingleSession()).
start();
}
catch (XMPPException ex)
{
throw new OperationFailedException(
"Failed to answer an incoming call"
, OperationFailedException.INTERNAL_ERROR);
}
}
/**
* Closes all active calls. And releases resources.
*/
public void shutdown()
{
logger.trace("Ending all active calls. ");
Iterator activeCalls = this.activeCallsRepository.getActiveCalls();
// this is fast, but events aren't triggered ...
//jingleManager.disconnectAllSessions();
//go through all active calls.
while(activeCalls.hasNext())
{
CallJabberImpl call = (CallJabberImpl) activeCalls.next();
Iterator callParticipants = call.getCallParticipants();
//go through all call participants and say bye to every one.
while (callParticipants.hasNext())
{
CallParticipant participant
= (CallParticipant) callParticipants.next();
try
{
this.hangupCallParticipant(participant);
}
catch (Exception ex)
{
logger.warn("Failed to properly hangup participant "
+ participant
, ex);
}
}
}
}
/**
* Implements method sessionRequested from JingleSessionRequestListener.
*
* @param jingleSessionRequest the session requested
*/
public void sessionRequested(JingleSessionRequest jingleSessionRequest)
{
IncomingJingleSession inJS;
logger.info("session requested ");
try
{
inJS = jingleSessionRequest.accept();
}
catch (XMPPException ex)
{
logger.error("Failed to accept incoming jingle request : " + ex);
return;
}
CallJabberImpl call = new CallJabberImpl(protocolProvider);
String from = jingleSessionRequest.getFrom();
// we remove the ressource information at ends if any, as it is for
// no meaning for the user
if (from.indexOf("/") > 0)
{
from = from.substring(0, from.indexOf("/"));
}
CallParticipantJabberImpl callParticipant
= new CallParticipantJabberImpl(from, call);
callParticipant.setJingleSession(inJS);
callParticipant.setState(CallParticipantState.INCOMING_CALL);
activeCallsRepository.addCall(call);
fireCallEvent(CallEvent.CALL_RECEIVED, call);
}
/**
* Implements method sessionCreated from CreatedJingleSessionListener.
*
* @param jingleSession the newly created jingle session
*/
public void sessionCreated(JingleSession jingleSession)
{
logger.info("session created : " + jingleSession);
jingleSession.addListener(this);
jingleSession.addMediaListener(this);
jingleSession.addStateListener(this);
jingleSession.addTransportListener(this);
}
/**
* Implements method mediaClosed from JingleMediaListener.
*
* @param payloadType payload supported by the closed media
*/
public void mediaClosed(PayloadType payloadType)
{
logger.info(" media closed ");
}
/**
* Implements method <tt>mediaEstablished</tt> from JingleMediaListener.
*
* @param payloadType payload used by the established media
*/
public void mediaEstablished(PayloadType payloadType)
{
logger.info("media established ");
}
/**
* Implements method <tt>transportClosed</tt> from JingleTransportListener.
*
* @param transportCandidate <tt>transportCandiate</tt> with which
* we were dealing
*/
public void transportClosed(TransportCandidate transportCandidate)
{
logger.info("transport closed ");
}
/**
* Implements method <tt>transportClosedOnError</tt> from JingleTransportListener.
*
* @param ex the exception accompagning this error
*/
public void transportClosedOnError(XMPPException ex)
{
logger.error("transport closed on error ", ex);
}
/**
* Implements method <tt>transportEstablished</tt> from JingleTransportListener.
*
* @param local local <tt>TransportCandidate</tt> for this transport link
* @param remote remote <tt>TransportCandidate</tt> for this transport link
*/
public void transportEstablished(TransportCandidate local,
TransportCandidate remote)
{
logger.info("transport established " + local + " -:- " + remote);
}
/**
* Implements method <tt>beforeChange</tt> from JingleSessionStateListener.
* This method is called before the change occurs in the session.
* We can cancel the change by throwing a <tt>JingleException</tt>
*
* @param oldState old state of the session
* @param newState state in which we will go
*
* @throws JingleException we have the ability to cancel a state change by
* throwing a <tt>JingleException</tt>
*/
public void beforeChange(JingleNegotiator.State oldState
, JingleNegotiator.State newState)
throws JingleNegotiator.JingleException
{
if (newState instanceof IncomingJingleSession.Active)
{
JingleSession session = (JingleSession) newState.getNegotiator();
CallParticipantJabberImpl callParticipant =
activeCallsRepository.findCallParticipant(session);
if (callParticipant == null)
{
return;
}
callParticipant.setState(CallParticipantState.CONNECTED);
}
else if (newState instanceof OutgoingJingleSession.Inviting)
{
JingleSession session = (JingleSession) newState.getNegotiator();
CallParticipantJabberImpl callParticipant =
activeCallsRepository.findCallParticipant(session);
if (callParticipant == null)
{
return;
}
callParticipant.setState(CallParticipantState.CONNECTING);
}
else if (newState instanceof OutgoingJingleSession.Pending)
{
JingleSession session = (JingleSession) newState.getNegotiator();
CallParticipantJabberImpl callParticipant =
activeCallsRepository.findCallParticipant(session);
if (callParticipant == null)
{
return;
}
callParticipant.setState(CallParticipantState.ALERTING_REMOTE_SIDE);
}
else if (newState instanceof OutgoingJingleSession.Active)
{
JingleSession session = (JingleSession) newState.getNegotiator();
CallParticipantJabberImpl callParticipant =
activeCallsRepository.findCallParticipant(session);
if (callParticipant == null)
{
return;
}
callParticipant.setState(CallParticipantState.CONNECTED);
}
if ((newState == null) && (oldState != null))
{ //hanging
JingleSession session = (JingleSession) oldState.getNegotiator();
CallParticipantJabberImpl callParticipant =
activeCallsRepository.findCallParticipant(session);
if (callParticipant == null)
{
logger.debug("Received a stray trying response.");
return;
}
try
{
hangupCallParticipant(callParticipant);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
/**
* Implements method <tt>afterChanged</tt> from JingleSessionStateListener.
* called when we are effectivly in the <tt>newState</tt>
*
* @param oldState old session state
* @param newState new session state
*/
public void afterChanged(JingleNegotiator.State oldState,
JingleNegotiator.State newState)
{
logger.info("session state changed : " + oldState + " => " + newState);
}
/**
* Implements <tt>sessionEstablished</tt> from <tt>JingleSessionListener</tt>
*
*
* @param payloadType the <tt>payloadType</tt> used for media in thi session
* @param remoteCandidate the remote end point of thi session
* @param localCandidate the local end point of this session
* @param jingleSession the session which is now fully established
*/
public void sessionEstablished(PayloadType payloadType,
TransportCandidate remoteCandidate,
TransportCandidate localCandidate,
JingleSession jingleSession)
{
logger.info("session established ");
}
/**
* Implements <tt>sessionDeclined</tt> from <tt>JingleSessionListener</tt>
*
* @param reason why the session has been declined
* @param jingleSession the declined session
*/
public void sessionDeclined(String reason, JingleSession jingleSession)
{
logger.info("session declined : " + reason);
}
/**
* Implements <tt>sessionRedirected</tt> from <tt>JingleSessionListener</tt>
*
* @param redirection redirection information
* @param jingleSession the session which redirected
*/
public void sessionRedirected(String redirection,
JingleSession jingleSession)
{
logger.info("session redirected : " + redirection);
}
/**
* Implements <tt>sessionClosed</tt> from <tt>JingleSessionListener</tt>
*
* @param reason why the session has been closed
* @param jingleSession the session which is closed
*/
public void sessionClosed(String reason, JingleSession jingleSession)
{
logger.info("session closed : " + reason);
}
/**
* Implements <tt>sessionClosedOnError</tt> from <tt>JingleSessionListener</tt>
*
* @param ex execption which caused the error
* @param jingleSession the session which is closed
*/
public void sessionClosedOnError(XMPPException ex,
JingleSession jingleSession)
{
logger.error("session closed on error ", ex);
}
/**
* Implements <tt>sessionMediaReceived</tt> from <tt>JingleSessionListener</tt>
*
* @param jingleSession the session where the media is established
* @param participant the participant for this media session
*/
public void sessionMediaReceived(JingleSession jingleSession,
String participant)
{
logger.info("session media received ");
}
}
|