-
Notifications
You must be signed in to change notification settings - Fork 974
/
Videobridge.java
1872 lines (1671 loc) · 66.1 KB
/
Videobridge.java
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
/*
* Copyright @ 2015 Atlassian Pty Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jitsi.videobridge;
import java.util.*;
import java.util.concurrent.atomic.*;
import java.util.regex.*;
import net.java.sip.communicator.impl.protocol.jabber.extensions.*;
import net.java.sip.communicator.impl.protocol.jabber.extensions.colibri.*;
import net.java.sip.communicator.impl.protocol.jabber.extensions.health.*;
import net.java.sip.communicator.impl.protocol.jabber.extensions.jingle.*;
import net.java.sip.communicator.service.shutdown.*;
import net.java.sip.communicator.util.*;
import org.ice4j.ice.harvest.*;
import org.ice4j.stack.*;
import org.jitsi.eventadmin.*;
import org.jitsi.impl.neomedia.transform.*;
import org.jitsi.osgi.*;
import org.jitsi.service.configuration.*;
import org.jitsi.service.libjitsi.*;
import org.jitsi.service.neomedia.*;
import org.jitsi.util.*;
import org.jitsi.util.Logger;
import org.jitsi.videobridge.health.*;
import org.jitsi.videobridge.octo.*;
import org.jitsi.videobridge.pubsub.*;
import org.jitsi.videobridge.xmpp.*;
import org.jitsi.xmpp.util.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.provider.*;
import org.jivesoftware.smackx.pubsub.*;
import org.jivesoftware.smackx.pubsub.provider.*;
import org.jxmpp.jid.*;
import org.jxmpp.jid.parts.*;
import org.osgi.framework.*;
/**
* Represents the Jitsi Videobridge which creates, lists and destroys
* {@link Conference} instances.
*
* @author Lyubomir Marinov
* @author Hristo Terezov
* @author Boris Grozev
*/
public class Videobridge
{
public static final String COLIBRI_CLASS = "colibriClass";
/**
* The name of configuration property used to specify default processing
* options passed as the second argument to
* {@link #handleColibriConferenceIQ(ColibriConferenceIQ, int)}.
*/
public static final String DEFAULT_OPTIONS_PROPERTY_NAME
= "org.jitsi.videobridge.defaultOptions";
/**
* The XML namespace of the <tt>TransportManager</tt> type to be initialized
* by <tt>Channel</tt> by default.
*/
private static String defaultTransportManager;
/**
* The name of the property which specifies the path to the directory in
* which media recordings will be stored.
*/
public static final String ENABLE_MEDIA_RECORDING_PNAME
= "org.jitsi.videobridge.ENABLE_MEDIA_RECORDING";
/**
* The <tt>Logger</tt> used by the <tt>Videobridge</tt> class and its
* instances to print debug information.
*/
private static final Logger logger = Logger.getLogger(Videobridge.class);
/**
* The name of the property which controls whether media recording is
* enabled.
*/
public static final String MEDIA_RECORDING_PATH_PNAME
= "org.jitsi.videobridge.MEDIA_RECORDING_PATH";
/**
* The name of the property which specifies the token used to authenticate
* requests to enable media recording.
*/
public static final String MEDIA_RECORDING_TOKEN_PNAME
= "org.jitsi.videobridge.MEDIA_RECORDING_TOKEN";
/**
* The optional flag which specifies to
* {@link #handleColibriConferenceIQ(ColibriConferenceIQ, int)} that
* <tt>ColibriConferenceIQ</tt>s can be accessed by any peer(not only by the
* focus that created the conference).
*/
public static final int OPTION_ALLOW_ANY_FOCUS = 2;
/**
* The optional flag which specifies to
* {@link #handleColibriConferenceIQ(ColibriConferenceIQ, int)} that
* <tt>ColibriConferenceIQ</tt>s without an associated conference focus are
* allowed.
*/
public static final int OPTION_ALLOW_NO_FOCUS = 1;
/**
* The pseudo-random generator which is to be used when generating
* {@link Conference} and {@link Channel} IDs in order to minimize busy
* waiting for the value of {@link System#currentTimeMillis()} to change.
*/
public static final Random RANDOM = new Random();
/**
* The REST-like HTTP/JSON API of Jitsi Videobridge.
*/
public static final String REST_API = "rest";
/**
* The (base) <tt>System</tt> and/or <tt>ConfigurationService</tt> property
* of the REST-like HTTP/JSON API of Jitsi Videobridge.
*/
public static final String REST_API_PNAME
= "org.jitsi.videobridge." + REST_API;
/**
* The property that specifies allowed entities for turning on graceful
* shutdown mode. For XMPP API this is "from" JID. In case of REST
* the source IP is being copied into the "from" field of the IQ.
*/
public static final String SHUTDOWN_ALLOWED_SOURCE_REGEXP_PNAME
= "org.jitsi.videobridge.shutdown.ALLOWED_SOURCE_REGEXP";
/**
* The property that specifies entities authorized to operate the bridge.
* For XMPP API this is "from" JID. In case of REST the source IP is being
* copied into the "from" field of the IQ.
*/
public static final String AUTHORIZED_SOURCE_REGEXP_PNAME
= "org.jitsi.videobridge.AUTHORIZED_SOURCE_REGEXP";
/**
* The XMPP API of Jitsi Videobridge.
*/
public static final String XMPP_API = "xmpp";
/**
* The (base) <tt>System</tt> and/or <tt>ConfigurationService</tt> property
* of the XMPP API of Jitsi Videobridge.
*/
public static final String XMPP_API_PNAME
= "org.jitsi.videobridge." + XMPP_API;
public static Collection<Videobridge> getVideobridges(
BundleContext bundleContext)
{
return ServiceUtils2.getServices(bundleContext, Videobridge.class);
}
/**
* The pattern used to filter entities that are allowed to operate
* the videobridge.
*/
private Pattern authorizedSourcePattern;
/**
* The (OSGi) <tt>BundleContext</tt> in which this <tt>Videobridge</tt> has
* been started.
*/
private BundleContext bundleContext;
/**
* The <tt>Conference</tt>s of this <tt>Videobridge</tt> mapped by their
* IDs.
*/
private final Map<String, Conference> conferences = new HashMap<>();
/**
* Default options passed as second argument to
* {@link #handleColibriConferenceIQ(ColibriConferenceIQ, int)}
*/
private int defaultProcessingOptions;
/**
* Indicates if this bridge instance has entered graceful shutdown mode.
*/
private boolean shutdownInProgress;
/**
* The pattern used to filter entities that are allowed to trigger graceful
* shutdown mode.
*/
private Pattern shutdownSourcePattern;
/**
* A class that holds some instance statistics.
*/
private final Statistics statistics = new Statistics();
/**
* Thread that checks expiration for conferences, contents, channels and
* execute expire procedure for any of them.
*/
private VideobridgeExpireThread videobridgeExpireThread;
/**
* Initializes a new <tt>Videobridge</tt> instance.
*/
public Videobridge()
{
videobridgeExpireThread = new VideobridgeExpireThread(this);
}
/**
* Initializes a new {@link Conference} instance with an ID unique to the
* <tt>Conference</tt> instances listed by this <tt>Videobridge</tt> and
* adds the new instance to the list of existing <tt>Conference</tt>
* instances. Optionally the new instance is owned by a specific conference
* focus i.e. further/future requests to manage the new instance must come
* from the specified <tt>focus</tt> or they will be ignored. If the focus
* is not specified this safety check is overridden.
*
* @param focus (optional) a <tt>String</tt> which specifies the JID of
* the conference focus which will own the new instance i.e. from whom
* further/future requests to manage the new instance must come or they will
* be ignored. Pass <tt>null</tt> to override this safety check.
* @param name world readable name of the conference to create.
* @param gid the optional "global" id of the conference.
* @return a new <tt>Conference</tt> instance with an ID unique to the
* <tt>Conference</tt> instances listed by this <tt>Videobridge</tt>
*/
public Conference createConference(Jid focus, Localpart name, String gid)
{
return this.createConference(focus, name, /* enableLogging */ true, gid);
}
/**
* Initializes a new {@link Conference} instance with an ID unique to the
* <tt>Conference</tt> instances listed by this <tt>Videobridge</tt> and
* adds the new instance to the list of existing <tt>Conference</tt>
* instances. Optionally the new instance is owned by a specific conference
* focus i.e. further/future requests to manage the new instance must come
* from the specified <tt>focus</tt> or they will be ignored. If the focus
* is not specified this safety check is overridden.
*
* @param focus (optional) a <tt>String</tt> which specifies the JID of
* the conference focus which will own the new instance i.e. from whom
* further/future requests to manage the new instance must come or they will
* be ignored. Pass <tt>null</tt> to override this safety check.
* @param name world readable name of the conference to create.
* @param enableLogging whether logging should be enabled or disabled for
* the {@link Conference}.
* @param gid the optional "global" id of the conference.
* @return a new <tt>Conference</tt> instance with an ID unique to the
* <tt>Conference</tt> instances listed by this <tt>Videobridge</tt>
*/
public Conference createConference(
Jid focus, Localpart name, boolean enableLogging, String gid)
{
Conference conference = null;
do
{
String id = generateConferenceID();
synchronized (conferences)
{
if (!conferences.containsKey(id))
{
conference
= new Conference(
this,
id,
focus,
name,
enableLogging,
gid);
conferences.put(id, conference);
}
}
}
while (conference == null);
// The method Videobridge.getConferenceCountString() should better
// be executed outside synchronized blocks in order to reduce the
// risks of causing deadlocks.
if (logger.isInfoEnabled())
{
logger.info(Logger.Category.STATISTICS,
"create_conf," + conference.getLoggingId()
+ " conf_name=" + name
+ ",logging=" + enableLogging
+ "," + getConferenceCountString());
}
return conference;
}
/**
* Enables graceful shutdown mode on this bridge instance and eventually
* starts the shutdown immediately if no conferences are currently being
* hosted. Otherwise bridge will shutdown once all conferences expire.
*/
private void enableGracefulShutdownMode()
{
if (!shutdownInProgress)
{
logger.info("Entered graceful shutdown mode");
}
this.shutdownInProgress = true;
maybeDoShutdown();
}
/**
* Expires a specific <tt>Conference</tt> of this <tt>Videobridge</tt> (i.e.
* if the specified <tt>Conference</tt> is not in the list of
* <tt>Conference</tt>s of this <tt>Videobridge</tt>, does nothing).
*
* @param conference the <tt>Conference</tt> to be expired by this
* <tt>Videobridge</tt>
*/
public void expireConference(Conference conference)
{
String id = conference.getID();
boolean expireConference;
synchronized (conferences)
{
if (conference.equals(conferences.get(id)))
{
conferences.remove(id);
expireConference = true;
}
else
expireConference = false;
}
if (expireConference)
conference.expire();
// Check if it's the time to shutdown now
maybeDoShutdown();
}
/**
* Generates a new <tt>Conference</tt> ID which is not guaranteed to be
* unique.
*
* @return a new <tt>Conference</tt> ID which is not guaranteed to be unique
*/
private String generateConferenceID()
{
return Long.toHexString(System.currentTimeMillis() + RANDOM.nextLong());
}
/**
* Returns the OSGi <tt>BundleContext</tt> in which this
* <tt>Videobridge</tt> is executing.
*
* @return the OSGi <tt>BundleContext</tt> in which this
* <tt>Videobridge</tt> is executing.
*/
public BundleContext getBundleContext()
{
return bundleContext;
}
/**
* Gets the statistics of this instance.
*
* @return the statistics of this instance.
*/
public Statistics getStatistics()
{
return statistics;
}
/**
* Gets the number of active <tt>Channel</tt>s in this <tt>Videobridge</tt>
* (across all active <tt>Conference</tt>s and active <tt>Content</tt>s).
*
* @return the number of active <tt>Channel</tt>s in this
* <tt>Videobridge</tt>
*/
public int getChannelCount()
{
int channelCount = 0;
for (Conference conference : getConferences())
{
if (conference != null && !conference.isExpired())
{
for (Content content : conference.getContents())
{
if (content != null && !content.isExpired())
{
channelCount += content.getChannelCount();
}
}
}
}
return channelCount;
}
/**
* Gets the <tt>ComponentImpl</tt> instances which implement the XMPP API of
* this <tt>Videobridge</tt>.
*
* @return the <tt>ComponentImpl</tt> instances which implement the XMPP API
* of this <tt>Videobridge</tt>
*/
public Collection<ComponentImpl> getComponents()
{
return ComponentImpl.getComponents(getBundleContext());
}
/**
* Gets an existing {@link Conference} with a specific ID and a specific
* conference focus.
*
* @param id the ID of the existing <tt>Conference</tt> to get
* @param focus (optional) the JID of the conference focus of the existing
* <tt>Conference</tt> to get. A <tt>Conference</tt> does not take orders
* from a (remote) entity other than the conference focus who has
* initialized it. Pass <tt>null</tt> if you want any participant to be able
* to modify the conference.
* @return an existing <tt>Conference</tt> with the specified ID and the
* specified conference focus or <tt>null</tt> if no <tt>Conference</tt>
* with the specified ID and the specified conference focus is known to this
* <tt>Videobridge</tt>
*/
public Conference getConference(String id, Jid focus)
{
Conference conference;
synchronized (conferences)
{
conference = conferences.get(id);
}
if (conference != null)
{
/*
* (Optional) A conference is owned by the focus who has initialized
* it and it may be managed by that focus only.
*/
Jid conferenceFocus = conference.getFocus();
// If no 'focus' was given as an argument or if conference is not
// owned by any 'conferenceFocus' then skip equals()
if (focus == null || conferenceFocus == null
|| focus.equals(conferenceFocus))
{
// It seems the conference is still active.
conference.touch();
}
else
{
conference = null;
}
}
return conference;
}
/**
* Gets the number of <tt>Conference</tt>s of this <tt>Videobridge</tt> that
* are not expired.
*
* @return the number of <tt>Conference</tt>s of this <tt>Videobridge</tt>
* that are not expired.
*/
public int getConferenceCount()
{
int sz = 0;
Conference[] cs = getConferences();
if (cs != null && cs.length != 0)
{
for (Conference c : cs)
{
if (c != null && !c.isExpired())
{
sz++;
}
}
}
return sz;
}
/**
* Gets the <tt>Conference</tt>s of this <tt>Videobridge</tt>.
*
* @return the <tt>Conference</tt>s of this <tt>Videobridge</tt>
*/
public Conference[] getConferences()
{
synchronized (conferences)
{
Collection<Conference> values = conferences.values();
return values.toArray(new Conference[values.size()]);
}
}
/**
* Returns the <tt>ConfigurationService</tt> used by this
* <tt>Videobridge</tt>.
*
* @return the <tt>ConfigurationService</tt> used by this
* <tt>Videobridge</tt>.
*/
public ConfigurationService getConfigurationService()
{
BundleContext bundleContext = getBundleContext();
if (bundleContext == null)
{
return null;
}
else
{
return
ServiceUtils2.getService(
bundleContext,
ConfigurationService.class);
}
}
/**
* Gets the XML namespace of the <tt>TransportManager</tt> type to be
* initialized by <tt>Channel</tt> by default.
*
* @return the XML namespace of the <tt>TransportManager</tt> type to be
* initialized by <tt>Channel</tt> by default
*/
public String getDefaultTransportManager()
{
synchronized (Videobridge.class)
{
if (defaultTransportManager == null)
{
BundleContext bundleContext = getBundleContext();
if (bundleContext != null)
{
ConfigurationService cfg
= ServiceUtils2.getService(
bundleContext,
ConfigurationService.class);
if (cfg != null)
{
defaultTransportManager
= cfg.getString(
Videobridge.class.getName()
+ ".defaultTransportManager");
}
}
if (!IceUdpTransportPacketExtension.NAMESPACE.equals(
defaultTransportManager)
&& !RawUdpTransportPacketExtension.NAMESPACE.equals(
defaultTransportManager))
{
defaultTransportManager
= IceUdpTransportPacketExtension.NAMESPACE;
}
}
return defaultTransportManager;
}
}
/**
* Returns the <tt>EventAdmin</tt> instance (to be) used by this
* <tt>Videobridge</tt>.
*
* @return the <tt>EventAdmin</tt> instance (to be) used by this
* <tt>Videobridge</tt>.
*/
public EventAdmin getEventAdmin()
{
BundleContext bundleContext = getBundleContext();
if (bundleContext == null)
return null;
else
return ServiceUtils2.getService(bundleContext, EventAdmin.class);
}
/**
* Handles a <tt>ColibriConferenceIQ</tt> stanza which represents a request.
*
* @param conferenceIQ the <tt>ColibriConferenceIQ</tt> stanza represents
* the request to handle
* @return an <tt>org.jivesoftware.smack.packet.IQ</tt> stanza which
* represents the response to the specified request or <tt>null</tt> to
* reply with <tt>feature-not-implemented</tt>
* @throws Exception to reply with <tt>internal-server-error</tt> to the
* specified request
*/
public IQ handleColibriConferenceIQ(ColibriConferenceIQ conferenceIQ)
throws Exception
{
return
handleColibriConferenceIQ(conferenceIQ, defaultProcessingOptions);
}
/**
* Checks whether a COLIBRI request from a specific source ({@code focus})
* with specific {@code options} should be accepted or not.
* @param focus the source of the request (i.e. the JID of the conference
* focus).
* @param options
* @return {@code true} if a COLIBRI request from focus should be accepted,
* given the specified {@code options}, and {@code false} otherwise.
*/
private boolean accept(Jid focus, int options)
{
if ((options & OPTION_ALLOW_ANY_FOCUS) > 0)
{
return true;
}
if (focus == null)
{
return (options & OPTION_ALLOW_NO_FOCUS) != 0;
}
if (authorizedSourcePattern != null)
{
return authorizedSourcePattern.matcher(focus).matches();
}
else
{
return true;
}
}
/**
* Handles a <tt>ColibriConferenceIQ</tt> stanza which represents a request.
*
* @param conferenceIQ the <tt>ColibriConferenceIQ</tt> stanza represents
* the request to handle
* @param options
* @return an <tt>org.jivesoftware.smack.packet.IQ</tt> stanza which
* represents the response to the specified request or <tt>null</tt> to
* reply with <tt>feature-not-implemented</tt>
* @throws Exception to reply with <tt>internal-server-error</tt> to the
* specified request
*/
public IQ handleColibriConferenceIQ(
ColibriConferenceIQ conferenceIQ,
int options)
throws Exception
{
Jid focus = conferenceIQ.getFrom();
Conference conference;
if (!accept(focus, options))
{
return IQUtils.createError(
conferenceIQ, XMPPError.Condition.not_authorized);
}
else
{
/*
* The presence of the id attribute in the conference element
* signals whether a new conference is to be created or an existing
* conference is to be modified.
*/
String id = conferenceIQ.getID();
if (id == null)
{
if (isShutdownInProgress())
{
return ColibriConferenceIQ
.createGracefulShutdownErrorResponse(conferenceIQ);
}
else
{
conference
= createConference(
focus,
conferenceIQ.getName(),
conferenceIQ.getGID());
if (conference == null)
{
return IQUtils.createError(
conferenceIQ,
XMPPError.Condition.internal_server_error,
"Failed to create new conference");
}
}
}
else
{
conference = getConference(id, focus);
if (conference == null)
{
return IQUtils.createError(
conferenceIQ,
XMPPError.Condition.bad_request,
"Conference not found for ID: " + id);
}
}
conference.setLastKnownFocus(conferenceIQ.getFrom());
}
ColibriConferenceIQ responseConferenceIQ = new ColibriConferenceIQ();
conference.describeShallow(responseConferenceIQ);
Set<String> channelBundleIdsToDescribe = new HashSet<>();
responseConferenceIQ.setGracefulShutdown(isShutdownInProgress());
ColibriConferenceIQ.Recording recordingIQ = conferenceIQ.getRecording();
if (recordingIQ != null)
{
String tokenIQ = recordingIQ.getToken();
if (tokenIQ != null)
{
String tokenConfig
= getConfigurationService().getString(
Videobridge.MEDIA_RECORDING_TOKEN_PNAME);
if (tokenIQ.equals(tokenConfig))
{
ColibriConferenceIQ.Recording.State recState
= recordingIQ.getState();
boolean recording
= conference.setRecording(
ColibriConferenceIQ.Recording.State.ON
.equals(recState)
|| ColibriConferenceIQ.Recording.State
.PENDING.equals(recState));
ColibriConferenceIQ.Recording responseRecordingIq
= new ColibriConferenceIQ.Recording(recState);
if (recording)
{
responseRecordingIq.setDirectory(
conference.getRecordingDirectory());
}
responseConferenceIQ.setRecording(responseRecordingIq);
}
}
}
// TODO(gp) Remove ColibriConferenceIQ.RTCPTerminationStrategy
for (ColibriConferenceIQ.Content contentIQ
: conferenceIQ.getContents())
{
/*
* The content element springs into existence whenever it gets
* mentioned, it does not need explicit creation (in contrast to
* the conference and channel elements).
*/
String contentName = contentIQ.getName();
Content content = conference.getOrCreateContent(contentName);
if (content == null)
{
return IQUtils.createError(
conferenceIQ,
XMPPError.Condition.internal_server_error,
"Failed to create new content for name: "
+ contentName);
}
ColibriConferenceIQ.Content responseContentIQ
= new ColibriConferenceIQ.Content(content.getName());
responseConferenceIQ.addContent(responseContentIQ);
for (ColibriConferenceIQ.Channel channelIQ
: contentIQ.getChannels())
{
ColibriConferenceIQ.OctoChannel octoChannelIQ
= channelIQ instanceof ColibriConferenceIQ.OctoChannel
? (ColibriConferenceIQ.OctoChannel) channelIQ
: null;
String channelID = channelIQ.getID();
int channelExpire = channelIQ.getExpire();
String channelBundleId = channelIQ.getChannelBundleId();
// Channel bundles mentioned in "channel" elements in the
// request should be included in the response.
channelBundleIdsToDescribe.add(channelBundleId);
RtpChannel channel;
boolean channelCreated = false;
String transportNamespace
= channelIQ.getTransport() != null ?
channelIQ.getTransport().getNamespace() : null;
/*
* The presence of the id attribute in the channel
* element signals whether a new channel is to be
* created or an existing channel is to be modified.
*/
if (channelID == null)
{
if (channelExpire == 0)
{
// An expire attribute in the channel element with
// value equal to zero requests the immediate
// expiration of the channel in question.
// Consequently, it does not make sense to have it in a
// channel allocation request.
return IQUtils.createError(
conferenceIQ,
XMPPError.Condition.bad_request,
"Channel expire request for empty ID");
}
channel
= content.createRtpChannel(
channelBundleId,
transportNamespace,
channelIQ.isInitiator(),
channelIQ.getRTPLevelRelayType(),
octoChannelIQ != null);
if (channel == null)
{
return IQUtils.createError(
conferenceIQ,
XMPPError.Condition.internal_server_error,
"Failed to allocate new RTP Channel");
}
channelCreated = true;
}
else
{
// Request for an existing channel.
channel
= (RtpChannel) content.getChannel(channelID);
if (channel == null)
{
if (channelExpire == 0)
{
// Channel already expired?
continue;
}
else
{
return IQUtils.createError(
conferenceIQ,
XMPPError.Condition.bad_request,
"No RTP channel found for ID: " + channelID);
}
}
}
if (channelExpire
!= ColibriConferenceIQ.Channel.EXPIRE_NOT_SPECIFIED)
{
if (channelExpire < 0)
{
return IQUtils.createError(
conferenceIQ,
XMPPError.Condition.bad_request,
"Invalid 'expire' value: " + channelExpire);
}
channel.setExpire(channelExpire);
/*
* If the request indicates that it wants the channel
* expired and the channel is indeed expired, then
* the request is valid and has correctly been acted upon.
*/
if ((channelExpire == 0) && channel.isExpired())
{
continue;
}
}
// endpoint
// The attribute endpoint is optional. If a value is not
// specified, then the Channel endpoint is to not be changed.
String endpoint = channelIQ.getEndpoint();
if (endpoint != null)
{
channel.setEndpoint(endpoint);
}
/*
* The attribute last-n is optional. If a value is not
* specified, then the Channel lastN is to not be changed.
*/
Integer lastN = channelIQ.getLastN();
if (lastN != null)
{
channel.setLastN(lastN);
}
// Packet delay - for automated testing purpose only
Integer packetDelay = channelIQ.getPacketDelay();
if (packetDelay != null)
{
channel.setPacketDelay(packetDelay);
}
/*
* XXX The attribute initiator is optional. If a value is not
* specified, then the Channel initiator is to be assumed
* default or to not be changed.
*/
Boolean initiator = channelIQ.isInitiator();
if (initiator != null)
{
channel.setInitiator(initiator);
}
else
{
initiator = true;
}
channel.setPayloadTypes(channelIQ.getPayloadTypes());
channel.setRtpHeaderExtensions(
channelIQ.getRtpHeaderExtensions());
channel.setDirection(channelIQ.getDirection());
channel.setRtpEncodingParameters(
channelIQ.getSources(), channelIQ.getSourceGroups());
if (channelBundleId != null)
{
TransportManager transportManager
= conference.getTransportManager(
channelBundleId,
true,
initiator);
transportManager.addChannel(channel);
}
channel.setTransport(channelIQ.getTransport());
if (octoChannelIQ != null)
{
if (channel instanceof OctoChannel)
{
((OctoChannel) channel)
.setRelayIds(octoChannelIQ.getRelays());
}
else
{
logger.warn(
"Channel type mismatch: requested Octo, found "
+ channel.getClass().getSimpleName());
}
}
/*
* Provide (a description of) the current state of the channel
* as part of the response.
*/
ColibriConferenceIQ.Channel responseChannelIQ
= new ColibriConferenceIQ.Channel();
channel.describe(responseChannelIQ);
responseContentIQ.addChannel(responseChannelIQ);
EventAdmin eventAdmin;
if (channelCreated && (eventAdmin = getEventAdmin()) != null)
{
eventAdmin.sendEvent(EventFactory.channelCreated(channel));
}
// XXX we might want to fire more precise events, like
// sourceGroupsChanged or PayloadTypesChanged, etc.
content.fireChannelChanged(channel);
}
for (ColibriConferenceIQ.SctpConnection sctpConnIq
: contentIQ.getSctpConnections())
{
String id = sctpConnIq.getID();
String endpointID = sctpConnIq.getEndpoint();