aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/jabber/CallJabberImpl.java
blob: fe4f4f7b45b9317c5bfc4471824eb33d35dd3782 (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
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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
/*
 * Jitsi, 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.lang.ref.*;
import java.util.*;

import net.java.sip.communicator.impl.protocol.jabber.extensions.colibri.*;
import net.java.sip.communicator.impl.protocol.jabber.extensions.jingle.*;
import net.java.sip.communicator.impl.protocol.jabber.jinglesdp.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.service.protocol.media.*;
import net.java.sip.communicator.util.*;

import org.jitsi.service.neomedia.*;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smackx.packet.*;

/**
 * A Jabber implementation of the <tt>Call</tt> abstract class encapsulating
 * Jabber jingle sessions.
 *
 * @author Emil Ivov
 * @author Lyubomir Marinov
 * @author Boris Grozev
 */
public class CallJabberImpl
    extends AbstractCallJabberGTalkImpl<CallPeerJabberImpl>
{
    /**
     * The <tt>Logger</tt> used by the <tt>CallJabberImpl</tt> class and its
     * instances for logging output.
     */
    private static final Logger logger = Logger.getLogger(CallJabberImpl.class);

    /**
     * The Jitsi Videobridge conference which the local peer represented by this
     * instance is a focus of.
     */
    private ColibriConferenceIQ colibri;

    /**
     * The shared <tt>CallPeerMediaHandler</tt> state which is to be used by the
     * <tt>CallPeer</tt>s of this <tt>Call</tt> which use {@link #colibri}.
     */
    private MediaHandler colibriMediaHandler;

    /**
     * Contains one ColibriStreamConnector for each <tt>MediaType</tt>
     */
    private final List<WeakReference<ColibriStreamConnector>>
        colibriStreamConnectors;

    /**
     * The entity ID of the Jitsi Videobridge to be utilized by this
     * <tt>Call</tt> for the purposes of establishing a server-assisted
     * telephony conference.
     */
    private String jitsiVideobridge;

    /**
     * Initializes a new <tt>CallJabberImpl</tt> instance.
     *
     * @param parentOpSet the {@link OperationSetBasicTelephonyJabberImpl}
     * instance in the context of which this call has been created.
     */
    protected CallJabberImpl(
            OperationSetBasicTelephonyJabberImpl parentOpSet)
    {
        super(parentOpSet);

        int mediaTypeValueCount = MediaType.values().length;

        colibriStreamConnectors
            = new ArrayList<WeakReference<ColibriStreamConnector>>(
                    mediaTypeValueCount);
        for (int i = 0; i < mediaTypeValueCount; i++)
            colibriStreamConnectors.add(null);

        //let's add ourselves to the calls repo. we are doing it ourselves just
        //to make sure that no one ever forgets.
        parentOpSet.getActiveCallsRepository().addCall(this);
    }

    /**
     * Closes a specific <tt>ColibriStreamConnector</tt> which is associated with
     * a <tt>MediaStream</tt> of a specific <tt>MediaType</tt> upon request from
     * a specific <tt>CallPeer</tt>.
     *
     * @param peer the <tt>CallPeer</tt> which requests the closing of the
     * specified <tt>colibriStreamConnector</tt>
     * @param mediaType the <tt>MediaType</tt> of the <tt>MediaStream</tt> with
     * which the specified <tt>colibriStreamConnector</tt> is associated
     * @param colibriStreamConnector the <tt>ColibriStreamConnector</tt> to close on
     * behalf of the specified <tt>peer</tt>
     */
    public void closeColibriStreamConnector(
            CallPeerJabberImpl peer,
            MediaType mediaType,
            ColibriStreamConnector colibriStreamConnector)
    {
        colibriStreamConnector.close();
        synchronized (colibriStreamConnectors)
        {
            int index = mediaType.ordinal();
            WeakReference<ColibriStreamConnector> weakReference
                    = colibriStreamConnectors.get(index);
            if (weakReference != null && colibriStreamConnector
                    .equals(weakReference.get()))
            {
                colibriStreamConnectors.set(index, null);
            }
        }
    }

    /**
     * {@inheritDoc}
     *
     * Sends a <tt>content</tt> message to each of the <tt>CallPeer</tt>s
     * associated with this <tt>CallJabberImpl</tt> in order to include/exclude
     * the &quot;isfocus&quot; attribute.
     */
    @Override
    protected void conferenceFocusChanged(boolean oldValue, boolean newValue)
    {
        try
        {
            Iterator<CallPeerJabberImpl> peers = getCallPeers();

            while (peers.hasNext())
            {
                CallPeerJabberImpl callPeer = peers.next();

                if (callPeer.getState() == CallPeerState.CONNECTED)
                    callPeer.sendCoinSessionInfo();
            }
        }
        finally
        {
            super.conferenceFocusChanged(oldValue, newValue);
        }
    }

    /**
     * Allocates colibri (conference) channels for a specific <tt>MediaType</tt>
     * to be used by a specific <tt>CallPeer</tt>.
     *
     * @param peer the <tt>CallPeer</tt> which is to use the allocated colibri
     * (conference) channels
     * @param contentMap the local and remote <tt>ContentPacketExtension</tt>s
     * which specify the <tt>MediaType</tt>s for which colibri (conference)
     * channels are to be allocated
     * @return a <tt>ColibriConferenceIQ</tt> which describes the allocated
     * colibri (conference) channels for the specified <tt>mediaTypes</tt> which
     * are to be used by the specified <tt>peer</tt>; otherwise, <tt>null</tt>
     */
    public ColibriConferenceIQ createColibriChannels(
            CallPeerJabberImpl peer,
            Map<ContentPacketExtension,ContentPacketExtension> contentMap)
        throws OperationFailedException
    {
        if (!getConference().isJitsiVideobridge())
            return null;

        /*
         * For a colibri conference to work properly, all CallPeers in the
         * conference must share one and the same CallPeerMediaHandler state
         * i.e. they must use a single set of MediaStreams as if there was a
         * single CallPeerMediaHandler.
         */
        CallPeerMediaHandlerJabberImpl peerMediaHandler
            = peer.getMediaHandler();

        if (peerMediaHandler.getMediaHandler() != colibriMediaHandler)
        {
            for (MediaType mediaType : MediaType.values())
            {
                if (peerMediaHandler.getStream(mediaType) != null)
                    return null;
            }
        }

        ProtocolProviderServiceJabberImpl protocolProvider
            = getProtocolProvider();
        String jitsiVideobridge
            = (colibri == null) ? getJitsiVideobridge() : colibri.getFrom();

        if ((jitsiVideobridge == null) || (jitsiVideobridge.length() == 0))
        {
            logger.error(
                    "Failed to allocate colibri channels: no videobridge"
                        + " found.");
            return null;
        }

        /*
         * The specified CallPeer will participate in the colibri conference
         * organized by this Call so it must use the shared CallPeerMediaHandler
         * state of all CallPeers in the same colibri conference.
         */
        if (colibriMediaHandler == null)
            colibriMediaHandler = new MediaHandler();
        peerMediaHandler.setMediaHandler(colibriMediaHandler);

        ColibriConferenceIQ conferenceRequest = new ColibriConferenceIQ();

        if (colibri != null)
            conferenceRequest.setID(colibri.getID());

        for (Map.Entry<ContentPacketExtension,ContentPacketExtension> e
                : contentMap.entrySet())
        {
            ContentPacketExtension localContent = e.getKey();
            ContentPacketExtension remoteContent = e.getValue();
            ContentPacketExtension cpe
                = (remoteContent == null) ? localContent : remoteContent;
            RtpDescriptionPacketExtension rdpe
                = cpe.getFirstChildOfType(
                        RtpDescriptionPacketExtension.class);
            String media = rdpe.getMedia();
            MediaType mediaType = MediaType.parseString(media);
            String contentName = mediaType.toString();
            ColibriConferenceIQ.Content contentRequest
                = new ColibriConferenceIQ.Content(contentName);

            conferenceRequest.addContent(contentRequest);

            boolean requestLocalChannel = true;

            if (colibri != null)
            {
                ColibriConferenceIQ.Content content
                    = colibri.getContent(contentName);

                if ((content != null) && (content.getChannelCount() > 0))
                    requestLocalChannel = false;
            }

            boolean peerIsInitiator = peer.isInitiator();

            if (requestLocalChannel)
            {
                ColibriConferenceIQ.Channel localChannelRequest
                    = new ColibriConferenceIQ.Channel();

                localChannelRequest.setEndpoint(protocolProvider.getOurJID());
                localChannelRequest.setInitiator(peerIsInitiator);

                for (PayloadTypePacketExtension ptpe : rdpe.getPayloadTypes())
                    localChannelRequest.addPayloadType(ptpe);
                setTransportOnChannel(peer, media, localChannelRequest);
                // DTLS-SRTP
                setDtlsEncryptionOnChannel(
                        jitsiVideobridge,
                        peer,
                        mediaType,
                        localChannelRequest);
                /*
                 * Since Jitsi Videobridge supports multiple Jingle transports,
                 * it is a good idea to indicate which one is expected on a
                 * channel.
                 */
                ensureTransportOnChannel(localChannelRequest, peer);
                contentRequest.addChannel(localChannelRequest);
            }

            ColibriConferenceIQ.Channel remoteChannelRequest
                = new ColibriConferenceIQ.Channel();

            remoteChannelRequest.setEndpoint(peer.getAddress());
            remoteChannelRequest.setInitiator(!peerIsInitiator);

            for (PayloadTypePacketExtension ptpe : rdpe.getPayloadTypes())
                remoteChannelRequest.addPayloadType(ptpe);
            setTransportOnChannel(
                    media,
                    localContent,
                    remoteContent,
                    peer,
                    remoteChannelRequest);
            // DTLS-SRTP
            setDtlsEncryptionOnChannel(
                    mediaType,
                    localContent,
                    remoteContent,
                    peer,
                    remoteChannelRequest);
            /*
             * Since Jitsi Videobridge supports multiple Jingle transports, it
             * is a good idea to indicate which one is expected on a channel.
             */
            ensureTransportOnChannel(remoteChannelRequest, peer);
            contentRequest.addChannel(remoteChannelRequest);
        }

        XMPPConnection connection = protocolProvider.getConnection();
        PacketCollector packetCollector
            = connection.createPacketCollector(
                    new PacketIDFilter(conferenceRequest.getPacketID()));

        conferenceRequest.setTo(jitsiVideobridge);
        conferenceRequest.setType(IQ.Type.GET);
        connection.sendPacket(conferenceRequest);

        Packet response
            = packetCollector.nextResult(
                    SmackConfiguration.getPacketReplyTimeout());

        packetCollector.cancel();

        if (response == null)
        {
            logger.error(
                    "Failed to allocate colibri channels: response is null."
                        + " Maybe the response timed out.");
            return null;
        }
        else if (response.getError() != null)
        {
            logger.error(
                    "Failed to allocate colibri channels: "
                        + response.getError());
            return null;
        }
        else if (!(response instanceof ColibriConferenceIQ))
        {
            logger.error(
                    "Failed to allocate colibri channels: response is not a"
                        + " colibri conference");
            return null;
        }

        ColibriConferenceIQ conferenceResponse = (ColibriConferenceIQ) response;
        String conferenceResponseID = conferenceResponse.getID();

        /*
         * Update the complete ColibriConferenceIQ representation maintained by
         * this instance with the information given by the (current) response.
         */
        {
            if (colibri == null)
            {
                colibri = new ColibriConferenceIQ();
                /*
                 * XXX We must remember the JID of the Jitsi Videobridge because
                 * (1) we do not want to re-discover it in every method
                 * invocation on this Call instance and (2) we want to use one
                 * and the same for all CallPeers within this Call instance.
                 */
                colibri.setFrom(conferenceResponse.getFrom());
            }

            String colibriID = colibri.getID();

            if (colibriID == null)
                colibri.setID(conferenceResponseID);
            else if (!colibriID.equals(conferenceResponseID))
                throw new IllegalStateException("conference.id");

            for (ColibriConferenceIQ.Content contentResponse
                    : conferenceResponse.getContents())
            {
                String contentName = contentResponse.getName();
                ColibriConferenceIQ.Content content
                    = colibri.getOrCreateContent(contentName);

                for (ColibriConferenceIQ.Channel channelResponse
                        : contentResponse.getChannels())
                {
                    int channelIndex = content.getChannelCount();

                    content.addChannel(channelResponse);
                    if (channelIndex == 0)
                    {
                        TransportManagerJabberImpl transportManager
                            = peerMediaHandler.getTransportManager();

                        transportManager
                            .isEstablishingConnectivityWithJitsiVideobridge
                                = true;
                        transportManager
                            .startConnectivityEstablishmentWithJitsiVideobridge
                                = true;

                        MediaType mediaType
                            = MediaType.parseString(contentName);

                        // DTLS-SRTP
                        addDtlsAdvertisedEncryptions(
                                peer,
                                channelResponse,
                                mediaType);
                    }
                }
            }
        }

        /*
         * Formulate the result to be returned to the caller which is a subset
         * of the whole conference information kept by this CallJabberImpl and
         * includes the remote channels explicitly requested by the method
         * caller and their respective local channels.
         */
        ColibriConferenceIQ conferenceResult = new ColibriConferenceIQ();

        conferenceResult.setFrom(colibri.getFrom());
        conferenceResult.setID(conferenceResponseID);

        for (Map.Entry<ContentPacketExtension,ContentPacketExtension> e
                : contentMap.entrySet())
        {
            ContentPacketExtension localContent = e.getKey();
            ContentPacketExtension remoteContent = e.getValue();
            ContentPacketExtension cpe
                = (remoteContent == null) ? localContent : remoteContent;
            MediaType mediaType = JingleUtils.getMediaType(cpe);
            ColibriConferenceIQ.Content contentResponse
                = conferenceResponse.getContent(mediaType.toString());

            if (contentResponse != null)
            {
                String contentName = contentResponse.getName();
                ColibriConferenceIQ.Content contentResult
                    = new ColibriConferenceIQ.Content(contentName);

                conferenceResult.addContent(contentResult);

                /*
                 * The local channel may have been allocated in a previous
                 * method call as part of the allocation of the first remote
                 * channel in the respective content. Anyway, the current method
                 * caller still needs to know about it.
                 */
                ColibriConferenceIQ.Content content
                    = colibri.getContent(contentName);
                ColibriConferenceIQ.Channel localChannel = null;

                if ((content != null) && (content.getChannelCount() > 0))
                {
                    localChannel = content.getChannel(0);
                    contentResult.addChannel(localChannel);
                }

                String localChannelID
                    = (localChannel == null) ? null : localChannel.getID();

                for (ColibriConferenceIQ.Channel channelResponse
                        : contentResponse.getChannels())
                {
                    if ((localChannelID == null)
                            || !localChannelID.equals(channelResponse.getID()))
                        contentResult.addChannel(channelResponse);
                }
            }
        }

        return conferenceResult;
    }

    /**
     * Initializes a <tt>ColibriStreamConnector</tt> on behalf of a specific
     * <tt>CallPeer</tt> to be used in association with a specific
     * <tt>ColibriConferenceIQ.Channel</tt> of a specific <tt>MediaType</tt>.
     *
     * @param peer the <tt>CallPeer</tt> which requests the initialization of a
     * <tt>ColibriStreamConnector</tt>
     * @param mediaType the <tt>MediaType</tt> of the stream which is to use the
     * initialized <tt>ColibriStreamConnector</tt> for RTP and RTCP traffic
     * @param channel the <tt>ColibriConferenceIQ.Channel</tt> to which RTP and
     * RTCP traffic is to be sent and from which such traffic is to be received
     * via the initialized <tt>ColibriStreamConnector</tt>
     * @param factory a <tt>StreamConnectorFactory</tt> implementation which is
     * to allocate the sockets to be used for RTP and RTCP traffic
     * @return a <tt>ColibriStreamConnector</tt> to be used for RTP and RTCP
     * traffic associated with the specified <tt>channel</tt>
     */
    public ColibriStreamConnector createColibriStreamConnector(
                CallPeerJabberImpl peer,
                MediaType mediaType,
                ColibriConferenceIQ.Channel channel,
                StreamConnectorFactory factory)
    {
        String channelID = channel.getID();

        if (channelID == null)
            throw new IllegalArgumentException("channel");

        if (colibri == null)
            throw new IllegalStateException("colibri");

        ColibriConferenceIQ.Content content
            = colibri.getContent(mediaType.toString());

        if (content == null)
            throw new IllegalArgumentException("mediaType");
        if ((content.getChannelCount() < 1)
                || !channelID.equals((channel = content.getChannel(0)).getID()))
            throw new IllegalArgumentException("channel");

        ColibriStreamConnector colibriStreamConnector;

        synchronized (colibriStreamConnectors)
        {
            int index = mediaType.ordinal();
            WeakReference<ColibriStreamConnector> weakReference
                = colibriStreamConnectors.get(index);

            colibriStreamConnector
                = (weakReference == null) ? null : weakReference.get();
            if (colibriStreamConnector == null)
            {
                StreamConnector streamConnector
                    = factory.createStreamConnector();

                if (streamConnector != null)
                {
                    colibriStreamConnector
                        = new ColibriStreamConnector(streamConnector);
                    colibriStreamConnectors.set(
                        index,
                        new WeakReference<ColibriStreamConnector>(
                            colibriStreamConnector));
                }
            }
        }

        return colibriStreamConnector;
    }

    /**
     * Expires specific (colibri) conference channels used by a specific
     * <tt>CallPeer</tt>.
     *
     * @param peer the <tt>CallPeer</tt> which uses the specified (colibri)
     * conference channels to be expired
     * @param conference a <tt>ColibriConferenceIQ</tt> which specifies the
     * (colibri) conference channels to be expired
     */
    public void expireColibriChannels(
            CallPeerJabberImpl peer,
            ColibriConferenceIQ conference)
    {
        // Formulate the ColibriConferenceIQ request which is to be sent.
        if (colibri != null)
        {
            String conferenceID = colibri.getID();

            if (conferenceID.equals(conference.getID()))
            {
                ColibriConferenceIQ conferenceRequest
                    = new ColibriConferenceIQ();

                conferenceRequest.setID(conferenceID);

                for (ColibriConferenceIQ.Content content
                        : conference.getContents())
                {
                    ColibriConferenceIQ.Content colibriContent
                        = colibri.getContent(content.getName());

                    if (colibriContent != null)
                    {
                        ColibriConferenceIQ.Content contentRequest
                            = conferenceRequest.getOrCreateContent(
                            colibriContent.getName());

                        for (ColibriConferenceIQ.Channel channel
                                : content.getChannels())
                        {
                            ColibriConferenceIQ.Channel colibriChannel
                                = colibriContent.getChannel(channel.getID());

                            if (colibriChannel != null)
                            {
                                ColibriConferenceIQ.Channel channelRequest
                                    = new ColibriConferenceIQ.Channel();

                                channelRequest.setExpire(0);
                                channelRequest.setID(colibriChannel.getID());
                                contentRequest.addChannel(channelRequest);
                            }
                        }
                    }
                }

                /*
                 * Remove the channels which are to be expired from the internal
                 * state of the conference managed by this CallJabberImpl.
                 */
                for (ColibriConferenceIQ.Content contentRequest
                        : conferenceRequest.getContents())
                {
                    ColibriConferenceIQ.Content colibriContent
                        = colibri.getContent(contentRequest.getName());

                    for (ColibriConferenceIQ.Channel channelRequest
                            : contentRequest.getChannels())
                    {
                        ColibriConferenceIQ.Channel colibriChannel
                            = colibriContent.getChannel(channelRequest.getID());

                        colibriContent.removeChannel(colibriChannel);

                        /*
                         * If the last remote channel is to be expired, expire
                         * the local channel as well.
                         */
                        if (colibriContent.getChannelCount() == 1)
                        {
                            colibriChannel = colibriContent.getChannel(0);

                            channelRequest = new ColibriConferenceIQ.Channel();
                            channelRequest.setExpire(0);
                            channelRequest.setID(colibriChannel.getID());
                            contentRequest.addChannel(channelRequest);

                            colibriContent.removeChannel(colibriChannel);

                            break;
                        }
                    }
                }

                /*
                 * At long last, send the ColibriConferenceIQ request to expire
                 * the channels.
                 */
                conferenceRequest.setTo(colibri.getFrom());
                conferenceRequest.setType(IQ.Type.SET);
                getProtocolProvider().getConnection().sendPacket(
                        conferenceRequest);
            }
        }
    }

    /**
     * Sends a <tt>ColibriConferenceIQ</tt> to the videobridge used by this
     * <tt>CallJabberImpl</tt>, in order to request the the direction of
     * the <tt>channel</tt> with ID <tt>channelID</tt> be set to
     * <tt>direction</tt>
     * @param channelID the ID of the <tt>channel</tt> for which to set the
     * direction.
     * @param mediaType the <tt>MediaType</tt> of the channel (we can deduce this
     * by searching the <tt>ColibriConferenceIQ</tt>, but it's more convenient
     * to have it)
     * @param direction the <tt>MediaDirection</tt> to set.
     */
    public void setChannelDirection(String channelID,
                                    MediaType mediaType,
                                    MediaDirection direction)
    {
        if ((colibri != null) && (channelID != null))
        {
            ColibriConferenceIQ.Content content
                = colibri.getContent(mediaType.toString());

            if (content != null)
            {
                ColibriConferenceIQ.Channel channel
                    = content.getChannel(channelID);

                /*
                 * Note that we send requests even when the local Channel's
                 * direction and the direction we are setting are the same. We
                 * can easily avoid this, but we risk not sending necessary
                 * packets if local Channel and the actual channel on the
                 * videobridge are out of sync.
                 */
                if (channel != null)
                {
                    ColibriConferenceIQ.Channel requestChannel
                        = new ColibriConferenceIQ.Channel();

                    requestChannel.setID(channelID);
                    requestChannel.setDirection(direction);

                    ColibriConferenceIQ.Content requestContent
                        = new ColibriConferenceIQ.Content();

                    requestContent.setName(mediaType.toString());
                    requestContent.addChannel(requestChannel);

                    ColibriConferenceIQ conferenceRequest
                        = new ColibriConferenceIQ();

                    conferenceRequest.setID(colibri.getID());
                    conferenceRequest.setTo(colibri.getFrom());
                    conferenceRequest.setType(IQ.Type.SET);
                    conferenceRequest.addContent(requestContent);

                    getProtocolProvider().getConnection().sendPacket(
                            conferenceRequest);
                }
            }
        }
    }

    /**
     * Creates a <tt>CallPeerJabberImpl</tt> from <tt>calleeJID</tt> and sends
     * them <tt>session-initiate</tt> IQ request.
     *
     * @param calleeJID the party that we would like to invite to this call.
     * @param discoverInfo any discovery information that we have for the jid
     * we are trying to reach and that we are passing in order to avoid having
     * to ask for it again.
     * @param sessionInitiateExtensions a collection of additional and optional
     * <tt>PacketExtension</tt>s to be added to the <tt>session-initiate</tt>
     * {@link JingleIQ} which is to init this <tt>CallJabberImpl</tt>
     * @param supportedTransports the XML namespaces of the jingle transports
     * to use.
     *
     * @return the newly created <tt>CallPeerJabberImpl</tt> corresponding to
     * <tt>calleeJID</tt>. All following state change events will be
     * delivered through this call peer.
     *
     * @throws OperationFailedException with the corresponding code if we fail
     *  to create the call.
     */
    public CallPeerJabberImpl initiateSession(
            String calleeJID,
            DiscoverInfo discoverInfo,
            Iterable<PacketExtension> sessionInitiateExtensions,
            Collection<String> supportedTransports)
        throws OperationFailedException
    {
        // create the session-initiate IQ
        CallPeerJabberImpl callPeer = new CallPeerJabberImpl(calleeJID, this);

        callPeer.setDiscoveryInfo(discoverInfo);

        addCallPeer(callPeer);

        callPeer.setState(CallPeerState.INITIATING_CALL);

        // If this was the first peer we added in this call, then the call is
        // new and we need to notify everyone of its creation.
        if (getCallPeerCount() == 1)
            parentOpSet.fireCallEvent(CallEvent.CALL_INITIATED, this);

        CallPeerMediaHandlerJabberImpl mediaHandler
            = callPeer.getMediaHandler();

        //set the supported transports before the transport manager is created
        mediaHandler.setSupportedTransports(supportedTransports);

        /* enable video if it is a video call */
        mediaHandler.setLocalVideoTransmissionEnabled(localVideoAllowed);
        /* enable remote-control if it is a desktop sharing session */
        mediaHandler.setLocalInputEvtAware(getLocalInputEvtAware());

        /*
         * Set call state to connecting so that the user interface would start
         * playing the tones. We do that here because we may be harvesting
         * STUN/TURN addresses in initiateSession() which would take a while.
         */
        callPeer.setState(CallPeerState.CONNECTING);

        // if initializing session fails, set peer to failed
        boolean sessionInitiated = false;

        try
        {
            callPeer.initiateSession(sessionInitiateExtensions);
            sessionInitiated = true;
        }
        finally
        {
            // if initialization throws an exception
            if (!sessionInitiated)
                callPeer.setState(CallPeerState.FAILED);
        }
        return callPeer;
    }

    /**
     * Updates the Jingle sessions for the <tt>CallPeer</tt>s of this
     * <tt>Call</tt>, to reflect the current state of the the video contents of
     * this <tt>Call</tt>. Sends a <tt>content-modify</tt>, <tt>content-add</tt>
     * or <tt>content-remove</tt> message to each of the current
     * <tt>CallPeer</tt>s.
     *
     * @throws OperationFailedException if a problem occurred during message
     * generation or there was a network problem
     */
    @Override
    public void modifyVideoContent()
        throws OperationFailedException
    {
        if (logger.isDebugEnabled())
            logger.debug("Updating video content for " + this);

        boolean change = false;
        for (CallPeerJabberImpl peer : getCallPeerList())
            change |= peer.sendModifyVideoContent();

        if (change)
            fireCallChangeEvent(
                    CallChangeEvent.CALL_PARTICIPANTS_CHANGE, null, null);
    }

    /**
     * Notifies this instance that a specific <tt>ColibriConferenceIQ</tt> has
     * been received.
     *
     * @param conferenceIQ the <tt>ColibriConferenceIQ</tt> which has been
     * received
     * @return <tt>true</tt> if the specified <tt>conferenceIQ</tt> was
     * processed by this instance and no further processing is to be performed
     * by other possible processors of <tt>ColibriConferenceIQ</tt>s; otherwise,
     * <tt>false</tt>. Because a <tt>ColibriConferenceIQ</tt> request sent from
     * the Jitsi Videobridge server to the application as its client concerns a
     * specific <tt>CallJabberImpl</tt> implementation, no further processing by
     * other <tt>CallJabberImpl</tt> instances is necessary once the
     * <tt>ColibriConferenceIQ</tt> is processed by the associated
     * <tt>CallJabberImpl</tt> instance.
     */
    boolean processColibriConferenceIQ(ColibriConferenceIQ conferenceIQ)
    {
        if (colibri == null)
        {
            /*
             * This instance has not set up any conference using the Jitsi
             * Videobridge server-side technology yet so it cannot be bothered
             * with related requests.
             */
            return false;
        }
        else if (conferenceIQ.getID().equals(colibri.getID()))
        {
            /*
             * Remove the local Channels (from the specified conferenceIQ) i.e.
             * the Channels on which the local peer/user is sending to the Jitsi
             * Videobridge server because they concern this Call only and not
             * its CallPeers.
             */
            for (MediaType mediaType : MediaType.values())
            {
                String contentName = mediaType.toString();
                ColibriConferenceIQ.Content content
                    = conferenceIQ.getContent(contentName);

                if (content != null)
                {
                    ColibriConferenceIQ.Content thisContent
                        = colibri.getContent(contentName);

                    if ((thisContent != null)
                            && (thisContent.getChannelCount() > 0))
                    {
                        ColibriConferenceIQ.Channel thisChannel
                            = thisContent.getChannel(0);
                        ColibriConferenceIQ.Channel channel
                            = content.getChannel(thisChannel.getID());

                        if (channel != null)
                            content.removeChannel(channel);
                    }
                }
            }

            for (CallPeerJabberImpl callPeer : getCallPeerList())
                callPeer.processColibriConferenceIQ(conferenceIQ);

            /*
             * We have removed the local Channels from the specified
             * conferenceIQ. Consequently, it is no longer the same and fit for
             * processing by other CallJabberImpl instances.
             */
            return true;
        }
        else
        {
            /*
             * This instance has set up a conference using the Jitsi Videobridge
             * server-side technology but it is not the one referred to by the
             * specified conferenceIQ i.e. the specified conferenceIQ does not
             * concern this instance.
             */
            return false;
        }
    }

    /**
     * Creates a new call peer and sends a RINGING response.
     *
     * @param jingleIQ the {@link JingleIQ} that created the session.
     *
     * @return the newly created {@link CallPeerJabberImpl} (the one that sent
     * the INVITE).
     */
    public CallPeerJabberImpl processSessionInitiate(JingleIQ jingleIQ)
    {
        // Use the IQs 'from', instead of the jingle 'initiator' field,
        // because we want to make sure that following IQs are sent with the
        // correct 'to'.
        String remoteParty = jingleIQ.getFrom();

        boolean autoAnswer = false;
        CallPeerJabberImpl attendant = null;
        OperationSetBasicTelephonyJabberImpl basicTelephony = null;

        CallPeerJabberImpl callPeer
            = new CallPeerJabberImpl(remoteParty, this, jingleIQ);

        addCallPeer(callPeer);

        /*
         * We've already sent ack to the specified session-initiate so if it has
         * been sent as part of an attended transfer, we have to hang up on the
         * attendant.
         */
        try
        {
            TransferPacketExtension transfer
                = (TransferPacketExtension)
                    jingleIQ.getExtension(
                            TransferPacketExtension.ELEMENT_NAME,
                            TransferPacketExtension.NAMESPACE);

            if (transfer != null)
            {
                String sid = transfer.getSID();

                if (sid != null)
                {
                    ProtocolProviderServiceJabberImpl protocolProvider
                        = getProtocolProvider();
                    basicTelephony
                        = (OperationSetBasicTelephonyJabberImpl)
                            protocolProvider.getOperationSet(
                                    OperationSetBasicTelephony.class);
                    CallJabberImpl attendantCall
                        = basicTelephony
                            .getActiveCallsRepository()
                                .findSID(sid);

                    if (attendantCall != null)
                    {
                        attendant = attendantCall.getPeer(sid);
                        if ((attendant != null)
                                && basicTelephony
                                    .getFullCalleeURI(attendant.getAddress())
                                        .equals(transfer.getFrom())
                                && protocolProvider.getOurJID().equals(
                                        transfer.getTo()))
                        {
                            //basicTelephony.hangupCallPeer(attendant);
                            autoAnswer = true;
                        }
                    }
                }
            }
        }
        catch (Throwable t)
        {
            logger.error(
                    "Failed to hang up on attendant"
                        + " as part of session transfer",
                    t);

            if (t instanceof ThreadDeath)
                throw (ThreadDeath) t;
        }

        CoinPacketExtension coin
            = (CoinPacketExtension)
                jingleIQ.getExtension(
                        CoinPacketExtension.ELEMENT_NAME,
                        CoinPacketExtension.NAMESPACE);

        if (coin != null)
        {
            boolean b
                = Boolean.parseBoolean(
                        (String)
                            coin.getAttribute(
                                    CoinPacketExtension.ISFOCUS_ATTR_NAME));

            callPeer.setConferenceFocus(b);
        }

        //before notifying about this call, make sure that it looks alright
        callPeer.processSessionInitiate(jingleIQ);

        // if paranoia is set, to accept the call we need to know that
        // the other party has support for media encryption
        if (getProtocolProvider().getAccountID().getAccountPropertyBoolean(
                ProtocolProviderFactory.MODE_PARANOIA, false)
            && callPeer.getMediaHandler().getAdvertisedEncryptionMethods()
                    .length
                == 0)
        {
            //send an error response;
            String reasonText
                = JabberActivator.getResources().getI18NString(
                        "service.gui.security.encryption.required");
            JingleIQ errResp
                = JinglePacketFactory.createSessionTerminate(
                        jingleIQ.getTo(),
                        jingleIQ.getFrom(),
                        jingleIQ.getSID(),
                        Reason.SECURITY_ERROR,
                        reasonText);

            callPeer.setState(CallPeerState.FAILED, reasonText);
            getProtocolProvider().getConnection().sendPacket(errResp);

            return null;
        }

        if (callPeer.getState() == CallPeerState.FAILED)
            return null;

        callPeer.setState( CallPeerState.INCOMING_CALL );

        // in case of attended transfer, auto answer the call
        if (autoAnswer)
        {
            /* answer directly */
            try
            {
                callPeer.answer();
            }
            catch(Exception e)
            {
                logger.info(
                        "Exception occurred while answer transferred call",
                        e);
                callPeer = null;
            }

            // hang up now
            try
            {
                basicTelephony.hangupCallPeer(attendant);
            }
            catch(OperationFailedException e)
            {
                logger.error(
                        "Failed to hang up on attendant as part of session"
                            + " transfer",
                        e);
            }

            return callPeer;
        }

        /* see if offer contains audio and video so that we can propose
         * option to the user (i.e. answer with video if it is a video call...)
         */
        List<ContentPacketExtension> offer
            = callPeer.getSessionIQ().getContentList();
        Map<MediaType, MediaDirection> directions
            = new HashMap<MediaType, MediaDirection>();

        directions.put(MediaType.AUDIO, MediaDirection.INACTIVE);
        directions.put(MediaType.VIDEO, MediaDirection.INACTIVE);

        for (ContentPacketExtension c : offer)
        {
            String contentName = c.getName();
            MediaDirection remoteDirection
                = JingleUtils.getDirection(c, callPeer.isInitiator());

            if (MediaType.AUDIO.toString().equals(contentName))
                directions.put(MediaType.AUDIO, remoteDirection);
            else if (MediaType.VIDEO.toString().equals(contentName))
                directions.put(MediaType.VIDEO, remoteDirection);
        }

        // If this was the first peer we added in this call, then the call is
        // new and we need to notify everyone of its creation.
        if (getCallPeerCount() == 1)
        {
            parentOpSet.fireCallEvent(
                    CallEvent.CALL_RECEIVED,
                    this,
                    directions);
        }

        // Manages auto answer with "audio only", or "audio/video" answer.
        OperationSetAutoAnswerJabberImpl autoAnswerOpSet
            = (OperationSetAutoAnswerJabberImpl)
                getProtocolProvider().getOperationSet(
                        OperationSetBasicAutoAnswer.class);

        if (autoAnswerOpSet != null)
            autoAnswerOpSet.autoAnswer(this, directions);

        return callPeer;
    }

    /**
     * Updates the state of the local DTLS-SRTP endpoint (i.e. the local
     * <tt>DtlsControl</tt> instance) from the state of the remote DTLS-SRTP
     * endpoint represented by a specific <tt>ColibriConferenceIQ.Channel</tt>.
     *
     * @param peer the <tt>CallPeer</tt> associated with the method invocation
     * @param channel the <tt>ColibriConferenceIQ.Channel</tt> which represents
     * the state of the remote DTLS-SRTP endpoint
     * @param mediaType the <tt>MediaType</tt> of the media to be transmitted
     * over the DTLS-SRTP session
     */
    private boolean addDtlsAdvertisedEncryptions(
            CallPeerJabberImpl peer,
            ColibriConferenceIQ.Channel channel,
            MediaType mediaType)
    {
        CallPeerMediaHandlerJabberImpl peerMediaHandler
            = peer.getMediaHandler();
        DtlsControl dtlsControl
            = (DtlsControl)
                peerMediaHandler.getSrtpControls().get(
                        mediaType,
                        SrtpControlType.DTLS_SRTP);

        if (dtlsControl != null)
        {
            dtlsControl.setSetup(
                    peer.isInitiator()
                        ? DtlsControl.Setup.ACTIVE
                        : DtlsControl.Setup.PASSIVE);
        }

        IceUdpTransportPacketExtension remoteTransport = channel.getTransport();

        return
            peerMediaHandler.addDtlsAdvertisedEncryptions(
                    true,
                    remoteTransport,
                    mediaType);
    }

    /**
     * Updates the state of the remote DTLS-SRTP endpoint represented by a
     * specific <tt>ColibriConferenceIQ.Channel</tt> from the state of the local
     * DTLS-SRTP endpoint. The specified <tt>channel</tt> is to be used by the
     * conference focus for the purposes of transmitting media between a remote
     * peer and the Jitsi Videobridge server.
     *
     * @param mediaType the <tt>MediaType</tt> of the media to be transmitted
     * over the DTLS-SRTP session
     * @param localContent the <tt>ContentPacketExtension</tt> of the local peer
     * in the negotiation between the local and the remote peers. If
     * <tt>remoteContent</tt> is <tt>null</tt>, represents an offer from the
     * local peer to the remote peer; otherwise, represents an answer from the
     * local peer to an offer from the remote peer.
     * @param remoteContent the <tt>ContentPacketExtension</tt>, if any, of the
     * remote peer in the negotiation between the local and the remote peers. If
     * <tt>null</tt>, <tt>localContent</tt> represents an offer from the local
     * peer to the remote peer; otherwise, <tt>localContent</tt> represents an
     * answer from the local peer to an offer from the remote peer
     * @param peer the <tt>CallPeer</tt> which represents the remote peer and
     * which is associated with the specified <tt>channel</tt>
     * @param channel the <tt>ColibriConferenceIQ.Channel</tt> which represents
     * the state of the remote DTLS-SRTP endpoint.
     */
    private void setDtlsEncryptionOnChannel(
            MediaType mediaType,
            ContentPacketExtension localContent,
            ContentPacketExtension remoteContent,
            CallPeerJabberImpl peer,
            ColibriConferenceIQ.Channel channel)
    {
        AccountID accountID = getProtocolProvider().getAccountID();

        if (accountID.getAccountPropertyBoolean(
                    ProtocolProviderFactory.DEFAULT_ENCRYPTION,
                    true)
                && accountID.isEncryptionProtocolEnabled(
                        SrtpControlType.DTLS_SRTP)
                && (remoteContent != null))
        {
            IceUdpTransportPacketExtension remoteTransport
                = remoteContent.getFirstChildOfType(
                        IceUdpTransportPacketExtension.class);

            if (remoteTransport != null)
            {
                List<DtlsFingerprintPacketExtension> remoteFingerprints
                    = remoteTransport.getChildExtensionsOfType(
                            DtlsFingerprintPacketExtension.class);

                if (!remoteFingerprints.isEmpty())
                {
                    IceUdpTransportPacketExtension localTransport
                        = ensureTransportOnChannel(channel, peer);

                    if (localTransport != null)
                    {
                        List<DtlsFingerprintPacketExtension> localFingerprints
                            = localTransport.getChildExtensionsOfType(
                                    DtlsFingerprintPacketExtension.class);

                        if (localFingerprints.isEmpty())
                        {
                            for (DtlsFingerprintPacketExtension remoteFingerprint
                                    : remoteFingerprints)
                            {
                                DtlsFingerprintPacketExtension localFingerprint
                                    = new DtlsFingerprintPacketExtension();

                                localFingerprint.setFingerprint(
                                        remoteFingerprint.getFingerprint());
                                localFingerprint.setHash(
                                        remoteFingerprint.getHash());
                                localTransport.addChildExtension(
                                        localFingerprint);
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * Updates the state of the remote DTLS-SRTP endpoint represented by a
     * specific <tt>ColibriConferenceIQ.Channel</tt> from the state of the local
     * DTLS-SRTP endpoint (i.e. the local <tt>DtlsControl</tt> instance). The
     * specified <tt>channel</tt> is to be used by the conference focus for the
     * purposes of transmitting media between the local peer and the Jitsi
     * Videobridge server.
     *
     * @param jitsiVideobridge the address/JID of the Jitsi Videobridge
     * @param peer the <tt>CallPeer</tt> associated with the method invocation
     * @param mediaType the <tt>MediaType</tt> of the media to be transmitted
     * over the DTLS-SRTP session
     * @param channel the <tt>ColibriConferenceIQ.Channel</tt> which represents
     * the state of the remote DTLS-SRTP endpoint.
     */
    private void setDtlsEncryptionOnChannel(
            String jitsiVideobridge,
            CallPeerJabberImpl peer,
            MediaType mediaType,
            ColibriConferenceIQ.Channel channel)
    {
        ProtocolProviderServiceJabberImpl protocolProvider
            = getProtocolProvider();
        AccountID accountID = protocolProvider.getAccountID();

        if (accountID.getAccountPropertyBoolean(
                    ProtocolProviderFactory.DEFAULT_ENCRYPTION,
                    true)
                && accountID.isEncryptionProtocolEnabled(
                        SrtpControlType.DTLS_SRTP)
                && protocolProvider.isFeatureSupported(
                        jitsiVideobridge,
                        ProtocolProviderServiceJabberImpl
                            .URN_XMPP_JINGLE_DTLS_SRTP))
        {
            CallPeerMediaHandlerJabberImpl mediaHandler
                = peer.getMediaHandler();
            DtlsControl dtlsControl
                = (DtlsControl)
                    mediaHandler.getSrtpControls().getOrCreate(
                            mediaType,
                            SrtpControlType.DTLS_SRTP);

            if (dtlsControl != null)
            {
                IceUdpTransportPacketExtension transport
                    = ensureTransportOnChannel(channel, peer);

                if (transport != null)
                    setDtlsEncryptionOnTransport(dtlsControl, transport);
            }
        }
    }

    /**
     * Sets the properties (i.e. fingerprint and hash function) of a specific
     * <tt>DtlsControl</tt> on the specific
     * <tt>IceUdpTransportPacketExtension</tt>.
     *
     * @param dtlsControl the <tt>DtlsControl</tt> the properties of which are
     * to be set on the specified <tt>localTransport</tt>
     * @param localTransport the <tt>IceUdpTransportPacketExtension</tt> on
     * which the properties of the specified <tt>dtlsControl</tt> are to be set
     */
    static void setDtlsEncryptionOnTransport(
            DtlsControl dtlsControl,
            IceUdpTransportPacketExtension localTransport)
    {
        String fingerprint = dtlsControl.getLocalFingerprint();
        String hash = dtlsControl.getLocalFingerprintHashFunction();

        DtlsFingerprintPacketExtension fingerprintPE
            = localTransport.getFirstChildOfType(
                    DtlsFingerprintPacketExtension.class);

        if (fingerprintPE == null)
        {
            fingerprintPE = new DtlsFingerprintPacketExtension();
            localTransport.addChildExtension(fingerprintPE);
        }
        fingerprintPE.setFingerprint(fingerprint);
        fingerprintPE.setHash(hash);
    }

    private void setTransportOnChannel(
            CallPeerJabberImpl peer,
            String media,
            ColibriConferenceIQ.Channel channel)
        throws OperationFailedException
    {
        PacketExtension transport
            = peer.getMediaHandler().getTransportManager().createTransport(
                    media);

        if (transport instanceof IceUdpTransportPacketExtension)
            channel.setTransport((IceUdpTransportPacketExtension) transport);
    }

    private void setTransportOnChannel(
            String media,
            ContentPacketExtension localContent,
            ContentPacketExtension remoteContent,
            CallPeerJabberImpl peer,
            ColibriConferenceIQ.Channel channel)
        throws OperationFailedException
    {
        if (remoteContent != null)
        {
            IceUdpTransportPacketExtension transport
                = remoteContent.getFirstChildOfType(
                        IceUdpTransportPacketExtension.class);

            channel.setTransport(
                    TransportManagerJabberImpl.cloneTransportAndCandidates(
                            transport));
        }
    }

    /**
     * Makes an attempt to ensure that a specific
     * <tt>ColibriConferenceIQ.Channel</tt> has a non-<tt>null</tt>
     * <tt>transport</tt> set. If the specified <tt>channel</tt> does not have
     * a <tt>transport</tt>, the method invokes the <tt>TransportManager</tt> of
     * the specified <tt>CallPeerJabberImpl</tt> to initialize a new
     * <tt>PacketExtension</tt>.
     *
     * @param channel the <tt>ColibriConferenceIQ.Channel</tt> to ensure the
     * <tt>transport</tt> on
     * @param peer the <tt>CallPeerJabberImpl</tt> which is associated with the
     * specified <tt>channel</tt> and which specifies the
     * <tt>TransportManager</tt> to be described in the specified
     * <tt>channel</tt>
     * @return the <tt>transport</tt> of the specified <tt>channel</tt>
     */
    private IceUdpTransportPacketExtension ensureTransportOnChannel(
            ColibriConferenceIQ.Channel channel,
            CallPeerJabberImpl peer)
    {
        IceUdpTransportPacketExtension transport
            = channel.getTransport();

        if (transport == null)
        {
            PacketExtension pe
                = peer
                    .getMediaHandler()
                        .getTransportManager()
                            .createTransportPacketExtension();

            if (pe instanceof IceUdpTransportPacketExtension)
            {
                transport = (IceUdpTransportPacketExtension) pe;
                channel.setTransport(transport);
            }
        }
        return transport;
    }

    /**
     * Gets the entity ID of the Jitsi Videobridge to be utilized by this
     * <tt>Call</tt> for the purposes of establishing a server-assisted
     * telephony conference.
     *
     * @return the entity ID of the Jitsi Videobridge to be utilized by this
     * <tt>Call</tt> for the purposes of establishing a server-assisted
     * telephony conference.
     */
    public String getJitsiVideobridge()
    {
        if ((this.jitsiVideobridge == null)
                && getConference().isJitsiVideobridge())
        {
            String jitsiVideobridge
                = getProtocolProvider().getJitsiVideobridge();

            if (jitsiVideobridge != null)
                this.jitsiVideobridge = jitsiVideobridge;
        }
        return this.jitsiVideobridge;
    }

    /**
     * {@inheritDoc}
     *
     * Implements
     * {@link net.java.sip.communicator.service.protocol.event.DTMFListener#toneReceived(net.java.sip.communicator.service.protocol.event.DTMFReceivedEvent)}
     *
     * Forwards DTMF events to the <tt>IncomingDTMF</tt> operation set, setting
     * this <tt>Call</tt> as the source.
     */
    @Override
    public void toneReceived(DTMFReceivedEvent evt)
    {
        OperationSetIncomingDTMF opSet
            = getProtocolProvider()
                .getOperationSet(OperationSetIncomingDTMF.class);

        if (opSet != null && opSet instanceof OperationSetIncomingDTMFJabberImpl)
        {
            // Re-fire the event using this Call as the source.
            ((OperationSetIncomingDTMFJabberImpl) opSet).toneReceived(
                    new DTMFReceivedEvent(
                            this,
                            evt.getValue(),
                            evt.getDuration(),
                            evt.getStart()));
        }
    }
}