-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
janus_videoroom.c
3949 lines (3841 loc) · 172 KB
/
janus_videoroom.c
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
/*! \file janus_videoroom.c
* \author Lorenzo Miniero <lorenzo@meetecho.com>
* \copyright GNU General Public License v3
* \brief Janus VideoRoom plugin
* \details This is a plugin implementing a videoconferencing SFU
* (Selective Forwarding Unit) for Janus, that is an audio/video router.
* This means that the plugin implements a virtual conferencing room peers
* can join and leave at any time. This room is based on a Publish/Subscribe
* pattern. Each peer can publish his/her own live audio/video feeds: this
* feed becomes an available stream in the room the other participants can
* attach to. This means that this plugin allows the realization of several
* different scenarios, ranging from a simple webinar (one speaker, several
* listeners) to a fully meshed video conference (each peer sending and
* receiving to and from all the others).
*
* For what concerns the subscriber side, there are two different ways to
* attach to a publisher's feed: a generic 'listener', which can attach to
* a single feed, and a more complex 'Multiplexed listener', which instead can
* attach to more feeds using the same PeerConnection. The generic 'listener'
* is the default, which means that if you want to watch more feeds at the
* same time, you'll need to create multiple 'listeners' to attach at any
* of them. The 'Multiplexed listener', instead, is a more complex alternative
* that exploits the so called RTCWEB 'Plan B', which multiplexes more
* streams on a single PeerConnection and in the SDP: while more efficient in terms of
* resources, though, this approach is experimental, and currently only
* available on Google Chrome, so use it wisely.
* \note As of now, work on Plan B is still going on, and as such its support in Janus
* is flaky to say the least. Don't try to attach as a Multiplexed listener or bad
* things will probably happen!
*
* Considering that this plugin allows for several different WebRTC PeerConnections
* to be on at the same time for the same peer (specifically, each peer
* potentially has 1 PeerConnection on for publishing and N on for subscriptions
* from other peers), each peer may need to attach several times to the same
* plugin for every stream: this means that each peer needs to have at least one
* handle active for managing its relation with the plugin (joining a room,
* leaving a room, muting/unmuting, publishing, receiving events), and needs
* to open a new one each time he/she wants to subscribe to a feed from
* another participant (or a single one in case a 'Multiplexed listener is used).
* The handle used for a subscription, however, would be logically a "slave"
* to the master one used for managing the room: this means that it cannot
* be used, for instance, to unmute in the room, as its only purpose would
* be to provide a context in which creating the sendonly PeerConnection
* for the subscription to the active participant.
*
* Rooms to make available are listed in the plugin configuration file.
* A pre-filled configuration file is provided in \c conf/janus.plugin.videoroom.cfg
* and includes a demo room for testing. The same plugin is also used
* dynamically (that is, with rooms created on the fly via API) in the
* Screen Sharing demo as well.
*
* To add more rooms or modify the existing one, you can use the following
* syntax:
*
* \verbatim
[<unique room ID>]
description = This is my awesome room
is_private = yes|no (private rooms don't appear when you do a 'list' request)
secret = <optional password needed for manipulating (e.g. destroying) the room>
pin = <optional password needed for joining the room>
require_pvtid = yes|no (whether subscriptions are required to provide a valid
a valid private_id to associate with a publisher, default=no)
publishers = <max number of concurrent senders> (e.g., 6 for a video
conference or 1 for a webinar)
bitrate = <max video bitrate for senders> (e.g., 128000)
fir_freq = <send a FIR to publishers every fir_freq seconds> (0=disable)
audiocodec = opus|isac32|isac16|pcmu|pcma|g722 (audio codec to force on publishers, default=opus)
videocodec = vp8|vp9|h264 (video codec to force on publishers, default=vp8)
audiolevel_ext = yes|no (whether the ssrc-audio-level RTP extension must be
negotiated/used or not for new publishers, default=yes)
audiolevel_event = yes|no (whether to emit event to other users or not)
audio_active_packets = 100 (number of packets with audio level, default=100, 2 seconds)
audio_level_average = 25 (average value of audio level, 127=muted, 0='too loud', default=25)
videoorientation_ext = yes|no (whether the video-orientation RTP extension must be
negotiated/used or not for new publishers, default=yes)
playoutdelay_ext = yes|no (whether the playout-delay RTP extension must be
negotiated/used or not for new publishers, default=yes)
record = true|false (whether this room should be recorded, default=false)
rec_dir = <folder where recordings should be stored, when enabled>
\endverbatim
*
* Note that recording will work with all codecs except iSAC.
*
* \section sfuapi Video Room API
*
* The Video Room API supports several requests, some of which are
* synchronous and some asynchronous. There are some situations, though,
* (invalid JSON, invalid request) which will always result in a
* synchronous error response even for asynchronous requests.
*
* \c create , \c destroy , \c exists, \c list, \c allowed, \c kick and
* and \c listparticipants are synchronous requests, which means you'll
* get a response directly within the context of the transaction.
* \c create allows you to create a new video room dynamically, as an
* alternative to using the configuration file; \c destroy removes a
* video room and destroys it, kicking all the users out as part of the
* process; \c exists allows you to check whether a specific video room
* exists; finally, \c list lists all the available rooms, while \c
* listparticipants lists all the participants of a specific room and
* their details.
*
* The \c join , \c joinandconfigure , \c configure , \c publish ,
* \c unpublish , \c start , \c pause , \c switch , \c stop , \c add ,
* \c remove and \c leave requests instead are all asynchronous, which
* means you'll get a notification about their success or failure in
* an event. \c join allows you to join a specific video room, specifying
* whether that specific PeerConnection will be used for publishing or
* watching; \c configure can be used to modify some of the participation
* settings (e.g., bitrate cap); \c joinandconfigure combines the previous
* two requests in a single one (just for publishers); \c publish can be
* used to start sending media to broadcast to the other participants,
* while \c unpublish does the opposite; \c start allows you to start
* receiving media from a publisher you've subscribed to previously by
* means of a \c join , while \c pause pauses the delivery of the media;
* the \c switch request can be used to change the source of the media
* flowing over a specific PeerConnection (e.g., I was watching Alice,
* I want to watch Bob now) without having to create a new handle for
* that; \c stop interrupts a viewer instance; finally, \c leave allows
* you to leave a video room for good.
*
* Notice that, in general, all users can create rooms. If you want to
* limit this functionality, you can configure an admin \c admin_key in
* the plugin settings. When configured, only "create" requests that
* include the correct \c admin_key value in an "admin_key" property
* will succeed, and will be rejected otherwise.
*
* Actual API docs: TBD.
*
* \ingroup plugins
* \ref plugins
*/
#include "plugin.h"
#include <jansson.h>
#include "../debug.h"
#include "../apierror.h"
#include "../config.h"
#include "../mutex.h"
#include "../rtp.h"
#include "../rtcp.h"
#include "../record.h"
#include "../sdp-utils.h"
#include "../utils.h"
#include <sys/types.h>
#include <sys/socket.h>
/* Plugin information */
#define JANUS_VIDEOROOM_VERSION 8
#define JANUS_VIDEOROOM_VERSION_STRING "0.0.8"
#define JANUS_VIDEOROOM_DESCRIPTION "This is a plugin implementing a videoconferencing SFU (Selective Forwarding Unit) for Janus, that is an audio/video router."
#define JANUS_VIDEOROOM_NAME "JANUS VideoRoom plugin"
#define JANUS_VIDEOROOM_AUTHOR "Meetecho s.r.l."
#define JANUS_VIDEOROOM_PACKAGE "janus.plugin.videoroom"
/* Plugin methods */
janus_plugin *create(void);
int janus_videoroom_init(janus_callbacks *callback, const char *config_path);
void janus_videoroom_destroy(void);
int janus_videoroom_get_api_compatibility(void);
int janus_videoroom_get_version(void);
const char *janus_videoroom_get_version_string(void);
const char *janus_videoroom_get_description(void);
const char *janus_videoroom_get_name(void);
const char *janus_videoroom_get_author(void);
const char *janus_videoroom_get_package(void);
void janus_videoroom_create_session(janus_plugin_session *handle, int *error);
struct janus_plugin_result *janus_videoroom_handle_message(janus_plugin_session *handle, char *transaction, json_t *message, json_t *jsep);
void janus_videoroom_setup_media(janus_plugin_session *handle);
void janus_videoroom_incoming_rtp(janus_plugin_session *handle, int video, char *buf, int len);
void janus_videoroom_incoming_rtcp(janus_plugin_session *handle, int video, char *buf, int len);
void janus_videoroom_incoming_data(janus_plugin_session *handle, char *buf, int len);
void janus_videoroom_slow_link(janus_plugin_session *handle, int uplink, int video);
void janus_videoroom_hangup_media(janus_plugin_session *handle);
void janus_videoroom_destroy_session(janus_plugin_session *handle, int *error);
json_t *janus_videoroom_query_session(janus_plugin_session *handle);
/* Plugin setup */
static janus_plugin janus_videoroom_plugin =
JANUS_PLUGIN_INIT (
.init = janus_videoroom_init,
.destroy = janus_videoroom_destroy,
.get_api_compatibility = janus_videoroom_get_api_compatibility,
.get_version = janus_videoroom_get_version,
.get_version_string = janus_videoroom_get_version_string,
.get_description = janus_videoroom_get_description,
.get_name = janus_videoroom_get_name,
.get_author = janus_videoroom_get_author,
.get_package = janus_videoroom_get_package,
.create_session = janus_videoroom_create_session,
.handle_message = janus_videoroom_handle_message,
.setup_media = janus_videoroom_setup_media,
.incoming_rtp = janus_videoroom_incoming_rtp,
.incoming_rtcp = janus_videoroom_incoming_rtcp,
.incoming_data = janus_videoroom_incoming_data,
.slow_link = janus_videoroom_slow_link,
.hangup_media = janus_videoroom_hangup_media,
.destroy_session = janus_videoroom_destroy_session,
.query_session = janus_videoroom_query_session,
);
/* Plugin creator */
janus_plugin *create(void) {
JANUS_LOG(LOG_VERB, "%s created!\n", JANUS_VIDEOROOM_NAME);
return &janus_videoroom_plugin;
}
/* Parameter validation */
static struct janus_json_parameter request_parameters[] = {
{"request", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter adminkey_parameters[] = {
{"admin_key", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter create_parameters[] = {
{"room", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"description", JSON_STRING, 0},
{"is_private", JANUS_JSON_BOOL, 0},
{"allowed", JSON_ARRAY, 0},
{"secret", JSON_STRING, 0},
{"pin", JSON_STRING, 0},
{"require_pvtid", JANUS_JSON_BOOL, 0},
{"bitrate", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"fir_freq", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"publishers", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"audiocodec", JSON_STRING, 0},
{"videocodec", JSON_STRING, 0},
{"audiolevel_ext", JANUS_JSON_BOOL, 0},
{"audiolevel_event", JANUS_JSON_BOOL, 0},
{"audio_active_packets", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"audio_level_average", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"videoorient_ext", JANUS_JSON_BOOL, 0},
{"playoutdelay_ext", JANUS_JSON_BOOL, 0},
{"record", JANUS_JSON_BOOL, 0},
{"rec_dir", JSON_STRING, 0},
{"permanent", JANUS_JSON_BOOL, 0}
};
static struct janus_json_parameter room_parameters[] = {
{"room", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE}
};
static struct janus_json_parameter destroy_parameters[] = {
{"room", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"permanent", JANUS_JSON_BOOL, 0}
};
static struct janus_json_parameter allowed_parameters[] = {
{"room", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"secret", JSON_STRING, 0},
{"action", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"allowed", JSON_ARRAY, 0}
};
static struct janus_json_parameter kick_parameters[] = {
{"room", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"secret", JSON_STRING, 0},
{"id", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE}
};
static struct janus_json_parameter join_parameters[] = {
{"room", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"ptype", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"audio", JANUS_JSON_BOOL, 0},
{"video", JANUS_JSON_BOOL, 0},
{"data", JANUS_JSON_BOOL, 0},
{"bitrate", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"record", JANUS_JSON_BOOL, 0},
{"filename", JSON_STRING, 0}
};
static struct janus_json_parameter publish_parameters[] = {
{"audio", JANUS_JSON_BOOL, 0},
{"video", JANUS_JSON_BOOL, 0},
{"data", JANUS_JSON_BOOL, 0},
{"bitrate", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"record", JANUS_JSON_BOOL, 0},
{"filename", JSON_STRING, 0},
{"display", JSON_STRING, 0}
};
static struct janus_json_parameter rtp_forward_parameters[] = {
{"room", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"publisher_id", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"video_port", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"video_ssrc", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"video_pt", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"audio_port", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"audio_ssrc", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"audio_pt", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"data_port", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"host", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter stop_rtp_forward_parameters[] = {
{"room", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"publisher_id", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"stream_id", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE}
};
static struct janus_json_parameter publisher_parameters[] = {
{"id", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"display", JSON_STRING, 0}
};
static struct janus_json_parameter configure_parameters[] = {
{"audio", JANUS_JSON_BOOL, 0},
{"video", JANUS_JSON_BOOL, 0},
{"data", JANUS_JSON_BOOL, 0}
};
static struct janus_json_parameter listener_parameters[] = {
{"feed", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"private_id", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"audio", JANUS_JSON_BOOL, 0},
{"video", JANUS_JSON_BOOL, 0},
{"data", JANUS_JSON_BOOL, 0}
};
/* Static configuration instance */
static janus_config *config = NULL;
static const char *config_folder = NULL;
static janus_mutex config_mutex;
/* Useful stuff */
static volatile gint initialized = 0, stopping = 0;
static gboolean notify_events = TRUE;
static janus_callbacks *gateway = NULL;
static GThread *handler_thread;
static GThread *watchdog;
static void *janus_videoroom_handler(void *data);
static void janus_videoroom_relay_rtp_packet(gpointer data, gpointer user_data);
static void janus_videoroom_relay_data_packet(gpointer data, gpointer user_data);
typedef enum janus_videoroom_p_type {
janus_videoroom_p_type_none = 0,
janus_videoroom_p_type_subscriber, /* Generic listener/subscriber */
janus_videoroom_p_type_publisher, /* Participant/publisher */
} janus_videoroom_p_type;
typedef struct janus_videoroom_message {
janus_plugin_session *handle;
char *transaction;
json_t *message;
json_t *jsep;
} janus_videoroom_message;
static GAsyncQueue *messages = NULL;
static janus_videoroom_message exit_message;
static void janus_videoroom_message_free(janus_videoroom_message *msg) {
if(!msg || msg == &exit_message)
return;
msg->handle = NULL;
g_free(msg->transaction);
msg->transaction = NULL;
if(msg->message)
json_decref(msg->message);
msg->message = NULL;
if(msg->jsep)
json_decref(msg->jsep);
msg->jsep = NULL;
g_free(msg);
}
/* Payload types we'll offer internally */
#define OPUS_PT 111
#define ISAC32_PT 104
#define ISAC16_PT 103
#define PCMU_PT 0
#define PCMA_PT 8
#define G722_PT 9
#define VP8_PT 96
#define VP9_PT 101
#define H264_PT 107
typedef enum janus_videoroom_audiocodec {
JANUS_VIDEOROOM_OPUS, /* Publishers will have to use OPUS */
JANUS_VIDEOROOM_ISAC_32K, /* Publishers will have to use ISAC 32K */
JANUS_VIDEOROOM_ISAC_16K, /* Publishers will have to use ISAC 16K */
JANUS_VIDEOROOM_PCMU, /* Publishers will have to use PCMU 8K */
JANUS_VIDEOROOM_PCMA, /* Publishers will have to use PCMA 8K */
JANUS_VIDEOROOM_G722 /* Publishers will have to use G.722 */
} janus_videoroom_audiocodec;
static const char *janus_videoroom_audiocodec_name(janus_videoroom_audiocodec acodec) {
switch(acodec) {
case JANUS_VIDEOROOM_OPUS:
return "opus";
case JANUS_VIDEOROOM_ISAC_32K:
return "isac32";
case JANUS_VIDEOROOM_ISAC_16K:
return "isac16";
case JANUS_VIDEOROOM_PCMU:
return "pcmu";
case JANUS_VIDEOROOM_PCMA:
return "pcma";
case JANUS_VIDEOROOM_G722:
return "g722";
default:
/* Shouldn't happen */
return "opus";
}
}
static int janus_videoroom_audiocodec_pt(janus_videoroom_audiocodec acodec) {
switch(acodec) {
case JANUS_VIDEOROOM_OPUS:
return OPUS_PT;
case JANUS_VIDEOROOM_ISAC_32K:
return ISAC32_PT;
case JANUS_VIDEOROOM_ISAC_16K:
return ISAC16_PT;
case JANUS_VIDEOROOM_PCMU:
return PCMU_PT;
case JANUS_VIDEOROOM_PCMA:
return PCMA_PT;
case JANUS_VIDEOROOM_G722:
return G722_PT;
default:
/* Shouldn't happen */
return OPUS_PT;
}
}
typedef enum janus_videoroom_videocodec {
JANUS_VIDEOROOM_VP8, /* Publishers will have to use VP8 */
JANUS_VIDEOROOM_VP9, /* Publishers will have to use VP9 */
JANUS_VIDEOROOM_H264 /* Publishers will have to use H264 */
} janus_videoroom_videocodec;
static const char *janus_videoroom_videocodec_name(janus_videoroom_videocodec vcodec) {
switch(vcodec) {
case JANUS_VIDEOROOM_VP8:
return "vp8";
case JANUS_VIDEOROOM_VP9:
return "vp9";
case JANUS_VIDEOROOM_H264:
return "h264";
default:
/* Shouldn't happen */
return "vp8";
}
}
static int janus_videoroom_videocodec_pt(janus_videoroom_videocodec vcodec) {
switch(vcodec) {
case JANUS_VIDEOROOM_VP8:
return VP8_PT;
case JANUS_VIDEOROOM_VP9:
return VP9_PT;
case JANUS_VIDEOROOM_H264:
return H264_PT;
default:
/* Shouldn't happen */
return VP8_PT;
}
}
typedef struct janus_videoroom {
guint64 room_id; /* Unique room ID */
gchar *room_name; /* Room description */
gchar *room_secret; /* Secret needed to manipulate (e.g., destroy) this room */
gchar *room_pin; /* Password needed to join this room, if any */
gboolean is_private; /* Whether this room is 'private' (as in hidden) or not */
gboolean require_pvtid; /* Whether subscriptions in this room require a private_id */
int max_publishers; /* Maximum number of concurrent publishers */
uint64_t bitrate; /* Global bitrate limit */
uint16_t fir_freq; /* Regular FIR frequency (0=disabled) */
janus_videoroom_audiocodec acodec; /* Audio codec to force on publishers*/
janus_videoroom_videocodec vcodec; /* Video codec to force on publishers*/
gboolean audiolevel_ext; /* Whether the ssrc-audio-level extension must be negotiated or not for new publishers */
gboolean audiolevel_event; /* Whether to emit event to other users about audiolevel */
int audio_active_packets; /* amount of packets with audio level for checkup */
int audio_level_average; /* average audio level */
gboolean videoorient_ext; /* Whether the video-orientation extension must be negotiated or not for new publishers */
gboolean playoutdelay_ext; /* Whether the playout-delay extension must be negotiated or not for new publishers */
gboolean record; /* Whether the feeds from publishers in this room should be recorded */
char *rec_dir; /* Where to save the recordings of this room, if enabled */
gint64 destroyed; /* Value to flag the room for destruction, done lazily */
GHashTable *participants; /* Map of potential publishers (we get listeners from them) */
GHashTable *private_ids; /* Map of existing private IDs */
gboolean check_tokens; /* Whether to check tokens when participants join (see below) */
GHashTable *allowed; /* Map of participants (as tokens) allowed to join */
janus_mutex participants_mutex;/* Mutex to protect room properties */
} janus_videoroom;
static GHashTable *rooms;
static janus_mutex rooms_mutex;
static GList *old_rooms;
static char *admin_key = NULL;
static void janus_videoroom_free(janus_videoroom *room);
typedef struct janus_videoroom_session {
janus_plugin_session *handle;
janus_videoroom_p_type participant_type;
gpointer participant;
gboolean started;
gboolean stopping;
volatile gint hangingup;
gint64 destroyed; /* Time at which this session was marked as destroyed */
} janus_videoroom_session;
static GHashTable *sessions;
static GList *old_sessions;
static janus_mutex sessions_mutex;
/* a host whose ports gets streamed rtp packets of the corresponding type. */
typedef struct janus_videoroom_rtp_forwarder {
gboolean is_video;
gboolean is_data;
uint32_t ssrc;
int payload_type;
struct sockaddr_in serv_addr;
} janus_videoroom_rtp_forwarder;
typedef struct janus_videoroom_participant {
janus_videoroom_session *session;
janus_videoroom *room; /* Room */
guint64 user_id; /* Unique ID in the room */
guint32 pvt_id; /* This is sent to the publisher for mapping purposes, but shouldn't be shared with others */
gchar *display; /* Display name (just for fun) */
gchar *sdp; /* The SDP this publisher negotiated, if any */
gboolean audio, video, data; /* Whether audio, video and/or data is going to be sent by this publisher */
guint32 audio_pt; /* Audio payload type (Opus) */
guint32 video_pt; /* Video payload type (depends on room configuration) */
guint32 audio_ssrc; /* Audio SSRC of this publisher */
guint32 video_ssrc; /* Video SSRC of this publisher */
guint8 audio_level_extmap_id; /* Audio level extmap ID */
guint8 video_orient_extmap_id; /* Video orientation extmap ID */
guint8 playout_delay_extmap_id; /* Playout delay extmap ID */
gboolean audio_active;
gboolean video_active;
int audio_active_packets; /* participants number of audio packets to accumulate */
int audio_dBov_sum; /* participants accumulated dBov value for audio level*/
gboolean data_active;
gboolean firefox; /* We send Firefox users a different kind of FIR */
uint64_t bitrate;
gint64 remb_startup;/* Incremental changes on REMB to reach the target at startup */
gint64 remb_latest; /* Time of latest sent REMB (to avoid flooding) */
gint64 fir_latest; /* Time of latest sent FIR (to avoid flooding) */
gint fir_seq; /* FIR sequence number */
gboolean recording_active; /* Whether this publisher has to be recorded or not */
gchar *recording_base; /* Base name for the recording (e.g., /path/to/filename, will generate /path/to/filename-audio.mjr and/or /path/to/filename-video.mjr */
janus_recorder *arc; /* The Janus recorder instance for this publisher's audio, if enabled */
janus_recorder *vrc; /* The Janus recorder instance for this publisher's video, if enabled */
janus_recorder *drc; /* The Janus recorder instance for this publisher's data, if enabled */
janus_mutex rec_mutex; /* Mutex to protect the recorders from race conditions */
GSList *listeners; /* Subscriptions to this publisher (who's watching this publisher) */
GSList *subscriptions; /* Subscriptions this publisher has created (who this publisher is watching) */
janus_mutex listeners_mutex;
GHashTable *rtp_forwarders;
janus_mutex rtp_forwarders_mutex;
int udp_sock; /* The udp socket on which to forward rtp packets */
gboolean kicked; /* Whether this participant has been kicked */
} janus_videoroom_participant;
static void janus_videoroom_participant_free(janus_videoroom_participant *p);
static void janus_videoroom_rtp_forwarder_free_helper(gpointer data);
static guint32 janus_videoroom_rtp_forwarder_add_helper(janus_videoroom_participant *p,
const gchar* host, int port, int pt, uint32_t ssrc, gboolean is_video, gboolean is_data);
typedef struct janus_videoroom_listener {
janus_videoroom_session *session;
janus_videoroom *room; /* Room */
janus_videoroom_participant *feed; /* Participant this listener is subscribed to */
guint32 pvt_id; /* Private ID of the participant that is subscribing (if available/provided) */
janus_rtp_switching_context context; /* Needed in case there are publisher switches on this listener */
gboolean audio, video, data; /* Whether audio, video and/or data must be sent to this publisher */
gboolean paused;
gboolean kicked; /* Whether this subscription belongs to a participant that has been kicked */
} janus_videoroom_listener;
static void janus_videoroom_listener_free(janus_videoroom_listener *l);
typedef struct janus_videoroom_rtp_relay_packet {
rtp_header *data;
gint length;
gboolean is_video;
uint32_t timestamp;
uint16_t seq_number;
} janus_videoroom_rtp_relay_packet;
/* Error codes */
#define JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR 499
#define JANUS_VIDEOROOM_ERROR_NO_MESSAGE 421
#define JANUS_VIDEOROOM_ERROR_INVALID_JSON 422
#define JANUS_VIDEOROOM_ERROR_INVALID_REQUEST 423
#define JANUS_VIDEOROOM_ERROR_JOIN_FIRST 424
#define JANUS_VIDEOROOM_ERROR_ALREADY_JOINED 425
#define JANUS_VIDEOROOM_ERROR_NO_SUCH_ROOM 426
#define JANUS_VIDEOROOM_ERROR_ROOM_EXISTS 427
#define JANUS_VIDEOROOM_ERROR_NO_SUCH_FEED 428
#define JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT 429
#define JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT 430
#define JANUS_VIDEOROOM_ERROR_INVALID_SDP_TYPE 431
#define JANUS_VIDEOROOM_ERROR_PUBLISHERS_FULL 432
#define JANUS_VIDEOROOM_ERROR_UNAUTHORIZED 433
#define JANUS_VIDEOROOM_ERROR_ALREADY_PUBLISHED 434
#define JANUS_VIDEOROOM_ERROR_NOT_PUBLISHED 435
#define JANUS_VIDEOROOM_ERROR_ID_EXISTS 436
#define JANUS_VIDEOROOM_ERROR_INVALID_SDP 437
static guint32 janus_videoroom_rtp_forwarder_add_helper(janus_videoroom_participant *p,
const gchar* host, int port, int pt, uint32_t ssrc, gboolean is_video, gboolean is_data) {
if(!p || !host) {
return 0;
}
janus_videoroom_rtp_forwarder *forward = g_malloc0(sizeof(janus_videoroom_rtp_forwarder));
forward->is_video = is_video;
forward->payload_type = pt;
forward->ssrc = ssrc;
forward->is_data = is_data;
forward->serv_addr.sin_family = AF_INET;
inet_pton(AF_INET, host, &(forward->serv_addr.sin_addr));
forward->serv_addr.sin_port = htons(port);
janus_mutex_lock(&p->rtp_forwarders_mutex);
guint32 stream_id = janus_random_uint32();
while(g_hash_table_lookup(p->rtp_forwarders, GUINT_TO_POINTER(stream_id)) != NULL) {
stream_id = janus_random_uint32();
}
g_hash_table_insert(p->rtp_forwarders, GUINT_TO_POINTER(stream_id), forward);
janus_mutex_unlock(&p->rtp_forwarders_mutex);
JANUS_LOG(LOG_VERB, "Added %s rtp_forward to participant %"SCNu64" host: %s:%d stream_id: %"SCNu32"\n",
is_data ? "data" : (is_video ? "video" : "audio"), p->user_id, host, port, stream_id);
return stream_id;
}
/* Convenience function for freeing a session */
static void session_free(gpointer data) {
if(data) {
janus_videoroom_session* session = (janus_videoroom_session*)data;
switch(session->participant_type) {
case janus_videoroom_p_type_publisher:
janus_videoroom_participant_free(session->participant);
break;
case janus_videoroom_p_type_subscriber:
janus_videoroom_listener_free(session->participant);
break;
default:
break;
}
session->handle = NULL;
g_free(session);
session = NULL;
}
}
static void janus_videoroom_rtp_forwarder_free_helper(gpointer data) {
if(data) {
janus_videoroom_rtp_forwarder* forward = (janus_videoroom_rtp_forwarder*)data;
if(forward) {
g_free(forward);
forward = NULL;
}
}
}
/* Convenience wrapper function for session_free that corresponds to GHRFunc() format for hash table cleanup */
static gboolean session_hash_table_remove(gpointer key, gpointer value, gpointer not_used) {
if(value) {
session_free(value);
}
return TRUE;
}
/* VideoRoom watchdog/garbage collector (sort of) */
static void *janus_videoroom_watchdog(void *data) {
JANUS_LOG(LOG_INFO, "VideoRoom watchdog started\n");
gint64 now = 0, room_now = 0;
while(g_atomic_int_get(&initialized) && !g_atomic_int_get(&stopping)) {
janus_mutex_lock(&sessions_mutex);
/* Iterate on all the participants/listeners and check if we need to remove any of them */
now = janus_get_monotonic_time();
if(old_sessions != NULL) {
GList *sl = old_sessions;
JANUS_LOG(LOG_HUGE, "Checking %d old VideoRoom sessions...\n", g_list_length(old_sessions));
while(sl) {
janus_videoroom_session *session = (janus_videoroom_session *)sl->data;
/* If we are stopping, their is no point to continue to iterate */
if(!initialized || stopping) {
break;
}
if(!session) {
sl = sl->next;
continue;
}
if(now-session->destroyed >= 5*G_USEC_PER_SEC) {
/* We're lazy and actually get rid of the stuff only after a few seconds */
JANUS_LOG(LOG_VERB, "Freeing old VideoRoom session\n");
GList *rm = sl->next;
old_sessions = g_list_delete_link(old_sessions, sl);
sl = rm;
g_hash_table_steal(sessions, session->handle);
session_free(session);
continue;
}
sl = sl->next;
}
}
janus_mutex_unlock(&sessions_mutex);
janus_mutex_lock(&rooms_mutex);
if(old_rooms != NULL) {
GList *rl = old_rooms;
room_now = janus_get_monotonic_time();
while(rl) {
janus_videoroom* room = (janus_videoroom*)rl->data;
if(!initialized || stopping){
break;
}
if(!room) {
rl = rl->next;
continue;
}
if(room_now - room->destroyed >= 5*G_USEC_PER_SEC) {
GList *rm = rl->next;
old_rooms = g_list_delete_link(old_rooms, rl);
rl = rm;
g_hash_table_remove(rooms, &room->room_id);
continue;
}
rl = rl->next;
}
}
janus_mutex_unlock(&rooms_mutex);
g_usleep(500000);
}
JANUS_LOG(LOG_INFO, "VideoRoom watchdog stopped\n");
return NULL;
}
/* Plugin implementation */
int janus_videoroom_init(janus_callbacks *callback, const char *config_path) {
if(g_atomic_int_get(&stopping)) {
/* Still stopping from before */
return -1;
}
if(callback == NULL || config_path == NULL) {
/* Invalid arguments */
return -1;
}
/* Read configuration */
char filename[255];
g_snprintf(filename, 255, "%s/%s.cfg", config_path, JANUS_VIDEOROOM_PACKAGE);
JANUS_LOG(LOG_VERB, "Configuration file: %s\n", filename);
config = janus_config_parse(filename);
config_folder = config_path;
if(config != NULL)
janus_config_print(config);
janus_mutex_init(&config_mutex);
rooms = g_hash_table_new_full(g_int64_hash, g_int64_equal,
(GDestroyNotify)g_free, (GDestroyNotify) janus_videoroom_free);
janus_mutex_init(&rooms_mutex);
sessions = g_hash_table_new(NULL, NULL);
janus_mutex_init(&sessions_mutex);
messages = g_async_queue_new_full((GDestroyNotify) janus_videoroom_message_free);
/* This is the callback we'll need to invoke to contact the gateway */
gateway = callback;
/* Parse configuration to populate the rooms list */
if(config != NULL) {
/* Any admin key to limit who can "create"? */
janus_config_item *key = janus_config_get_item_drilldown(config, "general", "admin_key");
if(key != NULL && key->value != NULL)
admin_key = g_strdup(key->value);
janus_config_item *events = janus_config_get_item_drilldown(config, "general", "events");
if(events != NULL && events->value != NULL)
notify_events = janus_is_true(events->value);
if(!notify_events && callback->events_is_enabled()) {
JANUS_LOG(LOG_WARN, "Notification of events to handlers disabled for %s\n", JANUS_VIDEOROOM_NAME);
}
/* Iterate on all rooms */
GList *cl = janus_config_get_categories(config);
while(cl != NULL) {
janus_config_category *cat = (janus_config_category *)cl->data;
if(cat->name == NULL || !strcasecmp(cat->name, "general")) {
cl = cl->next;
continue;
}
JANUS_LOG(LOG_VERB, "Adding video room '%s'\n", cat->name);
janus_config_item *desc = janus_config_get_item(cat, "description");
janus_config_item *priv = janus_config_get_item(cat, "is_private");
janus_config_item *secret = janus_config_get_item(cat, "secret");
janus_config_item *pin = janus_config_get_item(cat, "pin");
janus_config_item *req_pvtid = janus_config_get_item(cat, "require_pvtid");
janus_config_item *bitrate = janus_config_get_item(cat, "bitrate");
janus_config_item *maxp = janus_config_get_item(cat, "publishers");
janus_config_item *firfreq = janus_config_get_item(cat, "fir_freq");
janus_config_item *audiocodec = janus_config_get_item(cat, "audiocodec");
janus_config_item *videocodec = janus_config_get_item(cat, "videocodec");
janus_config_item *audiolevel_ext = janus_config_get_item(cat, "audiolevel_ext");
janus_config_item *audiolevel_event = janus_config_get_item(cat, "audiolevel_event");
janus_config_item *audio_active_packets = janus_config_get_item(cat, "audio_active_packets");
janus_config_item *audio_level_average = janus_config_get_item(cat, "audio_level_average");
janus_config_item *videoorient_ext = janus_config_get_item(cat, "videoorient_ext");
janus_config_item *playoutdelay_ext = janus_config_get_item(cat, "playoutdelay_ext");
janus_config_item *record = janus_config_get_item(cat, "record");
janus_config_item *rec_dir = janus_config_get_item(cat, "rec_dir");
/* Create the video room */
janus_videoroom *videoroom = g_malloc0(sizeof(janus_videoroom));
videoroom->room_id = g_ascii_strtoull(cat->name, NULL, 0);
char *description = NULL;
if(desc != NULL && desc->value != NULL && strlen(desc->value) > 0)
description = g_strdup(desc->value);
else
description = g_strdup(cat->name);
videoroom->room_name = description;
if(secret != NULL && secret->value != NULL) {
videoroom->room_secret = g_strdup(secret->value);
}
if(pin != NULL && pin->value != NULL) {
videoroom->room_pin = g_strdup(pin->value);
}
videoroom->is_private = priv && priv->value && janus_is_true(priv->value);
videoroom->require_pvtid = req_pvtid && req_pvtid->value && janus_is_true(req_pvtid->value);
videoroom->max_publishers = 3; /* FIXME How should we choose a default? */
if(maxp != NULL && maxp->value != NULL)
videoroom->max_publishers = atol(maxp->value);
if(videoroom->max_publishers < 0)
videoroom->max_publishers = 3; /* FIXME How should we choose a default? */
videoroom->bitrate = 0;
if(bitrate != NULL && bitrate->value != NULL)
videoroom->bitrate = atol(bitrate->value);
if(videoroom->bitrate > 0 && videoroom->bitrate < 64000)
videoroom->bitrate = 64000; /* Don't go below 64k */
videoroom->fir_freq = 0;
if(firfreq != NULL && firfreq->value != NULL)
videoroom->fir_freq = atol(firfreq->value);
videoroom->acodec = JANUS_VIDEOROOM_OPUS;
if(audiocodec && audiocodec->value) {
if(!strcasecmp(audiocodec->value, "opus"))
videoroom->acodec = JANUS_VIDEOROOM_OPUS;
else if(!strcasecmp(audiocodec->value, "isac32"))
videoroom->acodec = JANUS_VIDEOROOM_ISAC_32K;
else if(!strcasecmp(audiocodec->value, "isac16"))
videoroom->acodec = JANUS_VIDEOROOM_ISAC_16K;
else if(!strcasecmp(audiocodec->value, "pcmu"))
videoroom->acodec = JANUS_VIDEOROOM_PCMU;
else if(!strcasecmp(audiocodec->value, "pcma"))
videoroom->acodec = JANUS_VIDEOROOM_PCMA;
else if(!strcasecmp(audiocodec->value, "g722"))
videoroom->acodec = JANUS_VIDEOROOM_G722;
else {
JANUS_LOG(LOG_WARN, "Unsupported audio codec '%s', falling back to OPUS\n", audiocodec->value);
videoroom->acodec = JANUS_VIDEOROOM_OPUS;
}
}
videoroom->vcodec = JANUS_VIDEOROOM_VP8;
if(videocodec && videocodec->value) {
if(!strcasecmp(videocodec->value, "vp8"))
videoroom->vcodec = JANUS_VIDEOROOM_VP8;
else if(!strcasecmp(videocodec->value, "vp9"))
videoroom->vcodec = JANUS_VIDEOROOM_VP9;
else if(!strcasecmp(videocodec->value, "h264"))
videoroom->vcodec = JANUS_VIDEOROOM_H264;
else {
JANUS_LOG(LOG_WARN, "Unsupported video codec '%s', falling back to VP8\n", videocodec->value);
videoroom->vcodec = JANUS_VIDEOROOM_VP8;
}
}
videoroom->audiolevel_ext = TRUE;
if(audiolevel_ext != NULL && audiolevel_ext->value != NULL)
videoroom->audiolevel_ext = janus_is_true(audiolevel_ext->value);
videoroom->audiolevel_event = FALSE;
if(audiolevel_event != NULL && audiolevel_event->value != NULL)
videoroom->audiolevel_event = janus_is_true(audiolevel_event->value);
if(videoroom->audiolevel_event) {
videoroom->audio_active_packets = 100;
if(audio_active_packets != NULL && audio_active_packets->value != NULL){
if(atoi(audio_active_packets->value) > 0) {
videoroom->audio_active_packets = atoi(audio_active_packets->value);
} else {
JANUS_LOG(LOG_WARN, "Invalid audio_active_packets value, using default: %d\n", videoroom->audio_active_packets);
}
}
videoroom->audio_level_average = 25;
if(audio_level_average != NULL && audio_level_average->value != NULL) {
if(atoi(audio_level_average->value) > 0) {
videoroom->audio_level_average = atoi(audio_level_average->value);
} else {
JANUS_LOG(LOG_WARN, "Invalid audio_level_average value provided, using default: %d\n", videoroom->audio_level_average);
}
}
}
videoroom->videoorient_ext = TRUE;
if(videoorient_ext != NULL && videoorient_ext->value != NULL)
videoroom->videoorient_ext = janus_is_true(videoorient_ext->value);
videoroom->playoutdelay_ext = TRUE;
if(playoutdelay_ext != NULL && playoutdelay_ext->value != NULL)
videoroom->playoutdelay_ext = janus_is_true(playoutdelay_ext->value);
if(record && record->value) {
videoroom->record = janus_is_true(record->value);
}
if(rec_dir && rec_dir->value) {
videoroom->rec_dir = g_strdup(rec_dir->value);
}
videoroom->destroyed = 0;
janus_mutex_init(&videoroom->participants_mutex);
videoroom->participants = g_hash_table_new_full(g_int64_hash, g_int64_equal, (GDestroyNotify)g_free, NULL);
videoroom->private_ids = g_hash_table_new(NULL, NULL);
videoroom->check_tokens = FALSE; /* Static rooms can't have an "allowed" list yet, no hooks to the configuration file */
videoroom->allowed = g_hash_table_new_full(g_str_hash, g_str_equal, (GDestroyNotify)g_free, NULL);
janus_mutex_lock(&rooms_mutex);
g_hash_table_insert(rooms, janus_uint64_dup(videoroom->room_id), videoroom);
janus_mutex_unlock(&rooms_mutex);
JANUS_LOG(LOG_VERB, "Created videoroom: %"SCNu64" (%s, %s, %s/%s codecs, secret: %s, pin: %s, pvtid: %s)\n",
videoroom->room_id, videoroom->room_name,
videoroom->is_private ? "private" : "public",
janus_videoroom_audiocodec_name(videoroom->acodec),
janus_videoroom_videocodec_name(videoroom->vcodec),
videoroom->room_secret ? videoroom->room_secret : "no secret",
videoroom->room_pin ? videoroom->room_pin : "no pin",
videoroom->require_pvtid ? "required" : "optional");
if(videoroom->record) {
JANUS_LOG(LOG_VERB, " -- Room is going to be recorded in %s\n", videoroom->rec_dir ? videoroom->rec_dir : "the current folder");
}
cl = cl->next;
}
/* Done: we keep the configuration file open in case we get a "create" or "destroy" with permanent=true */
}
/* Show available rooms */
janus_mutex_lock(&rooms_mutex);
GHashTableIter iter;
gpointer value;
g_hash_table_iter_init(&iter, rooms);
while (g_hash_table_iter_next(&iter, NULL, &value)) {
janus_videoroom *vr = value;
JANUS_LOG(LOG_VERB, " ::: [%"SCNu64"][%s] %"SCNu64", max %d publishers, FIR frequency of %d seconds, %s audio codec, %s video codec\n",
vr->room_id, vr->room_name, vr->bitrate, vr->max_publishers, vr->fir_freq,
janus_videoroom_audiocodec_name(vr->acodec), janus_videoroom_videocodec_name(vr->vcodec));
}
janus_mutex_unlock(&rooms_mutex);
g_atomic_int_set(&initialized, 1);
GError *error = NULL;
/* Start the sessions watchdog */
watchdog = g_thread_try_new("videoroom watchdog", &janus_videoroom_watchdog, NULL, &error);
if(error != NULL) {
g_atomic_int_set(&initialized, 0);
JANUS_LOG(LOG_ERR, "Got error %d (%s) trying to launch the VideoRoom watchdog thread...\n", error->code, error->message ? error->message : "??");
janus_config_destroy(config);
return -1;
}
/* Launch the thread that will handle incoming messages */
handler_thread = g_thread_try_new("videoroom handler", janus_videoroom_handler, NULL, &error);
if(error != NULL) {
g_atomic_int_set(&initialized, 0);
JANUS_LOG(LOG_ERR, "Got error %d (%s) trying to launch the VideoRoom handler thread...\n", error->code, error->message ? error->message : "??");
janus_config_destroy(config);
return -1;
}
JANUS_LOG(LOG_INFO, "%s initialized!\n", JANUS_VIDEOROOM_NAME);
return 0;
}
void janus_videoroom_destroy(void) {
if(!g_atomic_int_get(&initialized))
return;
g_atomic_int_set(&stopping, 1);
g_async_queue_push(messages, &exit_message);
if(handler_thread != NULL) {
g_thread_join(handler_thread);
handler_thread = NULL;
}
if(watchdog != NULL) {
g_thread_join(watchdog);
watchdog = NULL;
}
/* FIXME We should destroy the sessions cleanly */
janus_mutex_lock(&sessions_mutex);
g_hash_table_foreach_remove(sessions, (GHRFunc)session_hash_table_remove, NULL);
g_hash_table_destroy(sessions);
sessions = NULL;
janus_mutex_unlock(&sessions_mutex);
janus_mutex_lock(&rooms_mutex);
g_hash_table_destroy(rooms);
rooms = NULL;
janus_mutex_unlock(&rooms_mutex);
janus_mutex_destroy(&rooms_mutex);
g_async_queue_unref(messages);
messages = NULL;
janus_config_destroy(config);
g_free(admin_key);
g_atomic_int_set(&initialized, 0);
g_atomic_int_set(&stopping, 0);
JANUS_LOG(LOG_INFO, "%s destroyed!\n", JANUS_VIDEOROOM_NAME);
}
int janus_videoroom_get_api_compatibility(void) {
/* Important! This is what your plugin MUST always return: don't lie here or bad things will happen */
return JANUS_PLUGIN_API_VERSION;
}
int janus_videoroom_get_version(void) {
return JANUS_VIDEOROOM_VERSION;
}