forked from meetecho/janus-gateway
-
Notifications
You must be signed in to change notification settings - Fork 1
/
janus.c
5542 lines (5328 loc) · 239 KB
/
janus.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.c
* \author Lorenzo Miniero <[email protected]>
* \copyright GNU General Public License v3
* \brief Janus core
* \details Implementation of the Janus core. This code takes care of
* the server initialization (command line/configuration) and setup,
* and makes use of the available transport plugins (by default HTTP,
* WebSockets, RabbitMQ, if compiled) and Janus protocol (a JSON-based
* protocol) to interact with the applications, whether they're web based
* or not. The core also takes care of bridging peers and plugins
* accordingly, in terms of both messaging and real-time media transfer
* via WebRTC.
*
* \ingroup core
* \ref core
*/
#include <dlfcn.h>
#include <dirent.h>
#include <net/if.h>
#include <netdb.h>
#include <signal.h>
#include <getopt.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
#ifdef HAVE_TURNRESTAPI
#include <curl/curl.h>
#endif
#include "janus.h"
#include "version.h"
#include "cmdline.h"
#include "config.h"
#include "apierror.h"
#include "debug.h"
#include "ip-utils.h"
#include "rtcp.h"
#include "auth.h"
#include "record.h"
#include "events.h"
#define JANUS_NAME "Janus WebRTC Server"
#define JANUS_AUTHOR "Meetecho s.r.l."
#define JANUS_SERVER_NAME "MyJanusInstance"
#ifdef __MACH__
#define SHLIB_EXT "0.dylib"
#else
#define SHLIB_EXT ".so"
#endif
static janus_config *config = NULL;
static char *config_file = NULL;
static char *configs_folder = NULL;
static GHashTable *transports = NULL;
static GHashTable *transports_so = NULL;
static GHashTable *eventhandlers = NULL;
static GHashTable *eventhandlers_so = NULL;
static GHashTable *loggers = NULL;
static GHashTable *loggers_so = NULL;
static GHashTable *plugins = NULL;
static GHashTable *plugins_so = NULL;
/* Daemonization */
static gboolean daemonize = FALSE;
static int pipefd[2];
#ifdef REFCOUNT_DEBUG
/* Reference counters debugging */
GHashTable *counters = NULL;
janus_mutex counters_mutex;
#endif
/* API secrets */
static char *api_secret = NULL, *admin_api_secret = NULL;
/* JSON parameters */
static int janus_process_error_string(janus_request *request, uint64_t session_id, const char *transaction, gint error, gchar *error_string);
static struct janus_json_parameter incoming_request_parameters[] = {
{"transaction", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"janus", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"id", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE}
};
static struct janus_json_parameter attach_parameters[] = {
{"plugin", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"opaque_id", JSON_STRING, 0},
};
static struct janus_json_parameter body_parameters[] = {
{"body", JSON_OBJECT, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter jsep_parameters[] = {
{"type", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"sdp", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"trickle", JANUS_JSON_BOOL, 0},
{"rid_order", JSON_STRING, 0},
{"e2ee", JANUS_JSON_BOOL, 0}
};
static struct janus_json_parameter add_token_parameters[] = {
{"token", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"plugins", JSON_ARRAY, 0}
};
static struct janus_json_parameter token_parameters[] = {
{"token", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter admin_parameters[] = {
{"transaction", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"janus", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter debug_parameters[] = {
{"debug", JANUS_JSON_BOOL, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter timeout_parameters[] = {
{"timeout", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE}
};
static struct janus_json_parameter level_parameters[] = {
{"level", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE}
};
static struct janus_json_parameter timestamps_parameters[] = {
{"timestamps", JANUS_JSON_BOOL, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter colors_parameters[] = {
{"colors", JANUS_JSON_BOOL, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter mnq_parameters[] = {
{"min_nack_queue", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE}
};
static struct janus_json_parameter nopt_parameters[] = {
{"nack_optimizations", JANUS_JSON_BOOL, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter nmt_parameters[] = {
{"no_media_timer", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE}
};
static struct janus_json_parameter st_parameters[] = {
{"slowlink_threshold", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE}
};
static struct janus_json_parameter ans_parameters[] = {
{"accept", JANUS_JSON_BOOL, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter querytransport_parameters[] = {
{"transport", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"request", JSON_OBJECT, 0}
};
static struct janus_json_parameter queryhandler_parameters[] = {
{"handler", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"request", JSON_OBJECT, 0}
};
static struct janus_json_parameter querylogger_parameters[] = {
{"logger", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"request", JSON_OBJECT, 0}
};
static struct janus_json_parameter messageplugin_parameters[] = {
{"plugin", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"request", JSON_OBJECT, 0}
};
static struct janus_json_parameter customevent_parameters[] = {
{"schema", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"data", JSON_OBJECT, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter customlogline_parameters[] = {
{"line", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"level", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE}
};
static struct janus_json_parameter text2pcap_parameters[] = {
{"folder", JSON_STRING, 0},
{"filename", JSON_STRING, 0},
{"truncate", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE}
};
static struct janus_json_parameter handleinfo_parameters[] = {
{"plugin_only", JANUS_JSON_BOOL, 0}
};
static struct janus_json_parameter resaddr_parameters[] = {
{"address", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter teststun_parameters[] = {
{"address", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"port", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"localport", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE}
};
/* Admin/Monitor helpers */
json_t *janus_admin_stream_summary(janus_ice_stream *stream);
json_t *janus_admin_component_summary(janus_ice_component *component);
/* IP addresses */
static gchar *local_ip = NULL;
gchar *janus_get_local_ip(void) {
return local_ip;
}
static GHashTable *public_ips_table = NULL;
static GList *public_ips = NULL;
gboolean public_ips_ipv4 = FALSE, public_ips_ipv6 = FALSE;
guint janus_get_public_ip_count(void) {
return public_ips_table ? g_hash_table_size(public_ips_table) : 0;
}
gchar *janus_get_public_ip(guint index) {
if (!janus_get_public_ip_count()) {
/* Fallback to the local IP, if we have no public one */
return local_ip;
}
if (index >= g_hash_table_size(public_ips_table)) {
index = g_hash_table_size(public_ips_table) - 1;
}
return (char *)g_list_nth(public_ips, index)->data;
}
void janus_add_public_ip(const gchar *ip) {
if(ip == NULL) {
return;
}
if(!public_ips_table) {
public_ips_table = g_hash_table_new_full(g_str_hash, g_str_equal, (GDestroyNotify)g_free, NULL);
}
if (g_hash_table_insert(public_ips_table, g_strdup(ip), NULL)) {
g_list_free(public_ips);
public_ips = g_hash_table_get_keys(public_ips_table);
}
/* Take note of whether we received at least one IPv4 and/or IPv6 address */
if(strchr(ip, ':')) {
public_ips_ipv6 = TRUE;
} else {
public_ips_ipv4 = TRUE;
}
}
gboolean janus_has_public_ipv4_ip(void) {
return public_ips_ipv4;
}
gboolean janus_has_public_ipv6_ip(void) {
return public_ips_ipv6;
}
static volatile gint stop = 0;
static gint stop_signal = 0;
gint janus_is_stopping(void) {
return g_atomic_int_get(&stop);
}
static GMainLoop *mainloop = NULL;
/* Public instance name */
static gchar *server_name = NULL;
static json_t *janus_create_message(const char *status, uint64_t session_id, const char *transaction) {
json_t *msg = json_object();
json_object_set_new(msg, "janus", json_string(status));
if(session_id > 0)
json_object_set_new(msg, "session_id", json_integer(session_id));
if(transaction != NULL)
json_object_set_new(msg, "transaction", json_string(transaction));
return msg;
}
/* The default timeout for sessions is 60 seconds: this means that, if
* we don't get any activity (i.e., no request) on this session for more
* than 60 seconds, then it's considered expired and we destroy it. That's
* why we have a keep-alive method in the API. This can be overridden in
* either janus.cfg/.jcfg or from the command line. Setting this to 0 will
* disable the timeout mechanism, which is NOT suggested as it may risk
* having orphaned sessions (sessions not controlled by any transport
* and never freed). Besides, notice that if you make this shorter than
* 30s, you'll have to update the timers in janus.js when the long
* polling mechanism is used and shorten them as well, or you'll risk
* incurring in unexpected timeouts (when HTTP is used in janus.js, the
* long poll is used as a keepalive mechanism). */
#define DEFAULT_SESSION_TIMEOUT 60
static uint session_timeout = DEFAULT_SESSION_TIMEOUT;
#define DEFAULT_RECLAIM_SESSION_TIMEOUT 0
static uint reclaim_session_timeout = DEFAULT_RECLAIM_SESSION_TIMEOUT;
/* We can programmatically change whether we want to accept new sessions
* or not: the default is of course TRUE, but we may want to temporarily
* change that in some cases, e.g., if we don't want the load on this
* server to grow too much, or because we're draining the server. */
static gboolean accept_new_sessions = TRUE;
/* We don't hold (trickle) candidates indefinitely either: by default, we
* only store them for 45 seconds. After that, they're discarded, in order
* to avoid leaks or orphaned media details. This means that, if for instance
* you're trying to set up a call with someone, and that someone only answers
* a minute later, the candidates you sent initially will be discarded and
* the call will fail. You can modify the default value in janus.jcfg */
#define DEFAULT_CANDIDATES_TIMEOUT 45
static uint candidates_timeout = DEFAULT_CANDIDATES_TIMEOUT;
/* By default we list dependencies details, but some may prefer not to */
static gboolean hide_dependencies = FALSE;
/* WebRTC encryption is obviously enabled by default. In the rare cases
* you want to disable it for debugging purposes, though, you can do
* that either via command line (-w) or in the main configuration file */
static gboolean webrtc_encryption = TRUE;
gboolean janus_is_webrtc_encryption_enabled(void) {
return webrtc_encryption;
}
/* Information */
static json_t *janus_info(const char *transaction) {
/* Prepare a summary on the Janus instance */
json_t *info = janus_create_message("server_info", 0, transaction);
json_object_set_new(info, "name", json_string(JANUS_NAME));
json_object_set_new(info, "version", json_integer(janus_version));
json_object_set_new(info, "version_string", json_string(janus_version_string));
json_object_set_new(info, "author", json_string(JANUS_AUTHOR));
json_object_set_new(info, "commit-hash", json_string(janus_build_git_sha));
json_object_set_new(info, "compile-time", json_string(janus_build_git_time));
json_object_set_new(info, "log-to-stdout", janus_log_is_stdout_enabled() ? json_true() : json_false());
json_object_set_new(info, "log-to-file", janus_log_is_logfile_enabled() ? json_true() : json_false());
if(janus_log_is_logfile_enabled())
json_object_set_new(info, "log-path", json_string(janus_log_get_logfile_path()));
#ifdef HAVE_SCTP
json_object_set_new(info, "data_channels", json_true());
#else
json_object_set_new(info, "data_channels", json_false());
#endif
json_object_set_new(info, "accepting-new-sessions", accept_new_sessions ? json_true() : json_false());
json_object_set_new(info, "session-timeout", json_integer(session_timeout));
json_object_set_new(info, "reclaim-session-timeout", json_integer(reclaim_session_timeout));
json_object_set_new(info, "candidates-timeout", json_integer(candidates_timeout));
json_object_set_new(info, "server-name", json_string(server_name ? server_name : JANUS_SERVER_NAME));
json_object_set_new(info, "local-ip", json_string(local_ip));
guint public_ip_count = janus_get_public_ip_count();
if(public_ip_count > 0) {
json_object_set_new(info, "public-ip", json_string(janus_get_public_ip(0)));
}
if(public_ip_count > 1) {
guint i;
json_t *ips = json_array();
for (i = 0; i < public_ip_count; i++) {
json_array_append_new(ips, json_string(janus_get_public_ip(i)));
}
json_object_set_new(info, "public-ips", ips);
}
json_object_set_new(info, "ipv6", janus_ice_is_ipv6_enabled() ? json_true() : json_false());
json_object_set_new(info, "ice-lite", janus_ice_is_ice_lite_enabled() ? json_true() : json_false());
json_object_set_new(info, "ice-tcp", janus_ice_is_ice_tcp_enabled() ? json_true() : json_false());
json_object_set_new(info, "full-trickle", janus_ice_is_full_trickle_enabled() ? json_true() : json_false());
json_object_set_new(info, "mdns-enabled", janus_ice_is_mdns_enabled() ? json_true() : json_false());
json_object_set_new(info, "min-nack-queue", json_integer(janus_get_min_nack_queue()));
json_object_set_new(info, "nack-optimizations", janus_is_nack_optimizations_enabled() ? json_true() : json_false());
json_object_set_new(info, "twcc-period", json_integer(janus_get_twcc_period()));
if(janus_get_dscp() > 0)
json_object_set_new(info, "dscp", json_integer(janus_get_dscp()));
json_object_set_new(info, "dtls-mtu", json_integer(janus_dtls_bio_agent_get_mtu()));
if(janus_ice_get_stun_server() != NULL) {
char server[255];
g_snprintf(server, 255, "%s:%"SCNu16, janus_ice_get_stun_server(), janus_ice_get_stun_port());
json_object_set_new(info, "stun-server", json_string(server));
}
if(janus_ice_get_turn_server() != NULL) {
char server[255];
g_snprintf(server, 255, "%s:%"SCNu16, janus_ice_get_turn_server(), janus_ice_get_turn_port());
json_object_set_new(info, "turn-server", json_string(server));
}
json_object_set_new(info, "static-event-loops", json_integer(janus_ice_get_static_event_loops()));
json_object_set_new(info, "api_secret", api_secret ? json_true() : json_false());
json_object_set_new(info, "auth_token", janus_auth_is_enabled() ? json_true() : json_false());
json_object_set_new(info, "event_handlers", janus_events_is_enabled() ? json_true() : json_false());
json_object_set_new(info, "opaqueid_in_api", janus_is_opaqueid_in_api_enabled() ? json_true() : json_false());
if(!webrtc_encryption)
json_object_set_new(info, "webrtc_encryption", json_false());
/* Dependencies */
if(!hide_dependencies) {
json_t *deps = json_object();
char glib2_version[20];
g_snprintf(glib2_version, sizeof(glib2_version), "%d.%d.%d", glib_major_version, glib_minor_version, glib_micro_version);
json_object_set_new(deps, "glib2", json_string(glib2_version));
json_object_set_new(deps, "jansson", json_string(JANSSON_VERSION));
json_object_set_new(deps, "libnice", json_string(libnice_version_string));
json_object_set_new(deps, "libsrtp", json_string(srtp_get_version_string()));
#ifdef HAVE_TURNRESTAPI
curl_version_info_data *curl_version = curl_version_info(CURLVERSION_NOW);
if(curl_version && curl_version->version)
json_object_set_new(deps, "libcurl", json_string(curl_version->version));
#endif
json_object_set_new(deps, "crypto", json_string(janus_get_ssl_version()));
json_object_set_new(info, "dependencies", deps);
}
/* Available transports */
json_t *t_data = json_object();
if(transports && g_hash_table_size(transports) > 0) {
GHashTableIter iter;
gpointer value;
g_hash_table_iter_init(&iter, transports);
while (g_hash_table_iter_next(&iter, NULL, &value)) {
janus_transport *t = value;
if(t == NULL) {
continue;
}
json_t *transport = json_object();
json_object_set_new(transport, "name", json_string(t->get_name()));
json_object_set_new(transport, "author", json_string(t->get_author()));
json_object_set_new(transport, "description", json_string(t->get_description()));
json_object_set_new(transport, "version_string", json_string(t->get_version_string()));
json_object_set_new(transport, "version", json_integer(t->get_version()));
json_object_set_new(t_data, t->get_package(), transport);
}
}
json_object_set_new(info, "transports", t_data);
/* Available event handlers */
json_t *e_data = json_object();
if(eventhandlers && g_hash_table_size(eventhandlers) > 0) {
GHashTableIter iter;
gpointer value;
g_hash_table_iter_init(&iter, eventhandlers);
while (g_hash_table_iter_next(&iter, NULL, &value)) {
janus_eventhandler *e = value;
if(e == NULL) {
continue;
}
json_t *eventhandler = json_object();
json_object_set_new(eventhandler, "name", json_string(e->get_name()));
json_object_set_new(eventhandler, "author", json_string(e->get_author()));
json_object_set_new(eventhandler, "description", json_string(e->get_description()));
json_object_set_new(eventhandler, "version_string", json_string(e->get_version_string()));
json_object_set_new(eventhandler, "version", json_integer(e->get_version()));
json_object_set_new(e_data, e->get_package(), eventhandler);
}
}
json_object_set_new(info, "events", e_data);
/* Available external loggers */
json_t *l_data = json_object();
if(loggers && g_hash_table_size(loggers) > 0) {
GHashTableIter iter;
gpointer value;
g_hash_table_iter_init(&iter, loggers);
while (g_hash_table_iter_next(&iter, NULL, &value)) {
janus_logger *l = value;
if(l == NULL) {
continue;
}
json_t *logger = json_object();
json_object_set_new(logger, "name", json_string(l->get_name()));
json_object_set_new(logger, "author", json_string(l->get_author()));
json_object_set_new(logger, "description", json_string(l->get_description()));
json_object_set_new(logger, "version_string", json_string(l->get_version_string()));
json_object_set_new(logger, "version", json_integer(l->get_version()));
json_object_set_new(l_data, l->get_package(), logger);
}
}
json_object_set_new(info, "loggers", l_data);
/* Available plugins */
json_t *p_data = json_object();
if(plugins && g_hash_table_size(plugins) > 0) {
GHashTableIter iter;
gpointer value;
g_hash_table_iter_init(&iter, plugins);
while (g_hash_table_iter_next(&iter, NULL, &value)) {
janus_plugin *p = value;
if(p == NULL) {
continue;
}
json_t *plugin = json_object();
json_object_set_new(plugin, "name", json_string(p->get_name()));
json_object_set_new(plugin, "author", json_string(p->get_author()));
json_object_set_new(plugin, "description", json_string(p->get_description()));
json_object_set_new(plugin, "version_string", json_string(p->get_version_string()));
json_object_set_new(plugin, "version", json_integer(p->get_version()));
json_object_set_new(p_data, p->get_package(), plugin);
}
}
json_object_set_new(info, "plugins", p_data);
return info;
}
/* Logging */
int janus_log_level = LOG_INFO;
gboolean janus_log_timestamps = FALSE;
gboolean janus_log_colors = FALSE;
char *janus_log_global_prefix = NULL;
int lock_debug = 0;
#ifdef REFCOUNT_DEBUG
int refcount_debug = 1;
#else
int refcount_debug = 0;
#endif
/*! \brief Signal handler (just used to intercept CTRL+C and SIGTERM) */
static void janus_handle_signal(int signum) {
stop_signal = signum;
switch(g_atomic_int_get(&stop)) {
case 0:
JANUS_PRINT("Stopping server, please wait...\n");
break;
case 1:
JANUS_PRINT("In a hurry? I'm trying to free resources cleanly, here!\n");
break;
default:
JANUS_PRINT("Ok, leaving immediately...\n");
break;
}
g_atomic_int_inc(&stop);
if(g_atomic_int_get(&stop) > 2)
exit(1);
if(mainloop && g_main_loop_is_running(mainloop))
g_main_loop_quit(mainloop);
}
/*! \brief Termination handler (atexit) */
static void janus_termination_handler(void) {
/* Free the instance name, if provided */
g_free(server_name);
/* Remove the PID file if we created it */
janus_pidfile_remove();
/* Close the logger */
janus_log_destroy();
/* Get rid of external loggers too, if any */
if(loggers != NULL && g_hash_table_size(loggers) > 0) {
g_hash_table_foreach(loggers, janus_logger_close, NULL);
g_hash_table_destroy(loggers);
}
if(loggers_so != NULL && g_hash_table_size(loggers_so) > 0) {
g_hash_table_foreach(loggers_so, janus_loggerso_close, NULL);
g_hash_table_destroy(loggers_so);
}
/* If we're daemonizing, we send an error code to the parent */
if(daemonize) {
int code = 1;
ssize_t res = 0;
do {
res = write(pipefd[1], &code, sizeof(int));
} while(res == -1 && errno == EINTR);
}
}
/** @name Transport plugin callback interface
* These are the callbacks implemented by the Janus core, as part of
* the janus_transport_callbacks interface. Everything the transport
* plugins send the core is handled here.
*/
///@{
void janus_transport_incoming_request(janus_transport *plugin, janus_transport_session *transport, void *request_id, gboolean admin, json_t *message, json_error_t *error);
void janus_transport_gone(janus_transport *plugin, janus_transport_session *transport);
gboolean janus_transport_is_api_secret_needed(janus_transport *plugin);
gboolean janus_transport_is_api_secret_valid(janus_transport *plugin, const char *apisecret);
gboolean janus_transport_is_auth_token_needed(janus_transport *plugin);
gboolean janus_transport_is_auth_token_valid(janus_transport *plugin, const char *token);
void janus_transport_notify_event(janus_transport *plugin, void *transport, json_t *event);
static janus_transport_callbacks janus_handler_transport =
{
.incoming_request = janus_transport_incoming_request,
.transport_gone = janus_transport_gone,
.is_api_secret_needed = janus_transport_is_api_secret_needed,
.is_api_secret_valid = janus_transport_is_api_secret_valid,
.is_auth_token_needed = janus_transport_is_auth_token_needed,
.is_auth_token_valid = janus_transport_is_auth_token_valid,
.events_is_enabled = janus_events_is_enabled,
.notify_event = janus_transport_notify_event,
};
static GAsyncQueue *requests = NULL;
static janus_request exit_message;
static GThreadPool *tasks = NULL;
void janus_transport_task(gpointer data, gpointer user_data);
///@}
/** @name Plugin callback interface
* These are the callbacks implemented by the Janus core, as part of
* the janus_callbacks interface. Everything the plugins send the
* core is handled here.
*/
///@{
int janus_plugin_push_event(janus_plugin_session *plugin_session, janus_plugin *plugin, const char *transaction, json_t *message, json_t *jsep);
json_t *janus_plugin_handle_sdp(janus_plugin_session *plugin_session, janus_plugin *plugin, const char *sdp_type, const char *sdp, gboolean restart);
void janus_plugin_relay_rtp(janus_plugin_session *plugin_session, janus_plugin_rtp *packet);
void janus_plugin_relay_rtcp(janus_plugin_session *plugin_session, janus_plugin_rtcp *packet);
void janus_plugin_relay_data(janus_plugin_session *plugin_session, janus_plugin_data *message);
void janus_plugin_send_pli(janus_plugin_session *plugin_session);
void janus_plugin_send_remb(janus_plugin_session *plugin_session, uint32_t bitrate);
void janus_plugin_close_pc(janus_plugin_session *plugin_session);
void janus_plugin_end_session(janus_plugin_session *plugin_session);
void janus_plugin_notify_event(janus_plugin *plugin, janus_plugin_session *plugin_session, json_t *event);
gboolean janus_plugin_auth_is_signature_valid(janus_plugin *plugin, const char *token);
gboolean janus_plugin_auth_signature_contains(janus_plugin *plugin, const char *token, const char *desc);
static janus_callbacks janus_handler_plugin =
{
.push_event = janus_plugin_push_event,
.relay_rtp = janus_plugin_relay_rtp,
.relay_rtcp = janus_plugin_relay_rtcp,
.relay_data = janus_plugin_relay_data,
.send_pli = janus_plugin_send_pli,
.send_remb = janus_plugin_send_remb,
.close_pc = janus_plugin_close_pc,
.end_session = janus_plugin_end_session,
.events_is_enabled = janus_events_is_enabled,
.notify_event = janus_plugin_notify_event,
.auth_is_signature_valid = janus_plugin_auth_is_signature_valid,
.auth_signature_contains = janus_plugin_auth_signature_contains,
};
///@}
/* Core Sessions */
static janus_mutex sessions_mutex;
static GHashTable *sessions = NULL;
static GMainContext *sessions_watchdog_context = NULL;
static void janus_ice_handle_dereference(janus_ice_handle *handle) {
if(handle)
janus_refcount_decrease(&handle->ref);
}
static void janus_session_free(const janus_refcount *session_ref) {
janus_session *session = janus_refcount_containerof(session_ref, janus_session, ref);
/* This session can be destroyed, free all the resources */
if(session->ice_handles != NULL) {
g_hash_table_destroy(session->ice_handles);
session->ice_handles = NULL;
}
if(session->source != NULL) {
janus_request_destroy(session->source);
session->source = NULL;
}
g_free(session);
}
static janus_request *janus_session_get_request(janus_session *session) {
if(session == NULL)
return NULL;
janus_mutex_lock(&session->mutex);
janus_request *source = session->source;
if(source != NULL && !g_atomic_int_get(&source->destroyed)) {
janus_refcount_increase(&source->ref);
} else {
source = NULL;
}
janus_mutex_unlock(&session->mutex);
return source;
}
static void janus_request_unref(janus_request *request) {
if(request)
janus_refcount_decrease(&request->ref);
}
static gboolean janus_check_sessions(gpointer user_data) {
if(session_timeout < 1 && reclaim_session_timeout < 1) /* Session timeouts are disabled */
return G_SOURCE_CONTINUE;
janus_mutex_lock(&sessions_mutex);
if(sessions && g_hash_table_size(sessions) > 0) {
GHashTableIter iter;
gpointer value;
g_hash_table_iter_init(&iter, sessions);
while (g_hash_table_iter_next(&iter, NULL, &value)) {
janus_session *session = (janus_session *) value;
if (!session || g_atomic_int_get(&session->destroyed)) {
continue;
}
gint64 now = janus_get_monotonic_time();
if ((session_timeout > 0 && (now - session->last_activity >= (gint64)session_timeout * G_USEC_PER_SEC) &&
!g_atomic_int_compare_and_exchange(&session->timeout, 0, 1)) ||
((g_atomic_int_get(&session->transport_gone) && now - session->last_activity >= (gint64)reclaim_session_timeout * G_USEC_PER_SEC) &&
!g_atomic_int_compare_and_exchange(&session->timeout, 0, 1))) {
JANUS_LOG(LOG_INFO, "Timeout expired for session %"SCNu64"...\n", session->session_id);
/* Mark the session as over, we'll deal with it later */
janus_session_handles_clear(session);
/* Notify the transport */
janus_request *source = janus_session_get_request(session);
if(source) {
json_t *event = janus_create_message("timeout", session->session_id, NULL);
/* Send this to the transport client and notify the session's over */
source->transport->send_message(source->instance, NULL, FALSE, event);
source->transport->session_over(source->instance, session->session_id, TRUE, FALSE);
}
janus_request_unref(source);
/* Notify event handlers as well */
if(janus_events_is_enabled())
janus_events_notify_handlers(JANUS_EVENT_TYPE_SESSION, JANUS_EVENT_SUBTYPE_NONE,
session->session_id, "timeout", NULL);
/* FIXME Is this safe? apparently it causes hash table errors on the console */
g_hash_table_iter_remove(&iter);
janus_session_destroy(session);
}
}
}
janus_mutex_unlock(&sessions_mutex);
return G_SOURCE_CONTINUE;
}
static gpointer janus_sessions_watchdog(gpointer user_data) {
GMainLoop *loop = (GMainLoop *) user_data;
GMainContext *watchdog_context = g_main_loop_get_context(loop);
GSource *timeout_source;
timeout_source = g_timeout_source_new_seconds(2);
g_source_set_callback(timeout_source, janus_check_sessions, watchdog_context, NULL);
g_source_attach(timeout_source, watchdog_context);
g_source_unref(timeout_source);
JANUS_LOG(LOG_INFO, "Sessions watchdog started\n");
g_main_loop_run(loop);
JANUS_LOG(LOG_INFO, "Sessions watchdog stopped\n");
return NULL;
}
janus_session *janus_session_create(guint64 session_id) {
janus_session *session = NULL;
if(session_id == 0) {
while(session_id == 0) {
session_id = janus_random_uint64();
session = janus_session_find(session_id);
if(session != NULL) {
/* Session ID already taken, try another one */
janus_refcount_decrease(&session->ref);
session_id = 0;
}
}
}
session = (janus_session *)g_malloc(sizeof(janus_session));
JANUS_LOG(LOG_INFO, "Creating new session: %"SCNu64"; %p\n", session_id, session);
session->session_id = session_id;
janus_refcount_init(&session->ref, janus_session_free);
session->source = NULL;
g_atomic_int_set(&session->destroyed, 0);
g_atomic_int_set(&session->timeout, 0);
g_atomic_int_set(&session->transport_gone, 0);
session->last_activity = janus_get_monotonic_time();
session->ice_handles = NULL;
janus_mutex_init(&session->mutex);
janus_mutex_lock(&sessions_mutex);
g_hash_table_insert(sessions, janus_uint64_dup(session->session_id), session);
janus_mutex_unlock(&sessions_mutex);
return session;
}
janus_session *janus_session_find(guint64 session_id) {
janus_mutex_lock(&sessions_mutex);
janus_session *session = g_hash_table_lookup(sessions, &session_id);
if(session != NULL) {
/* A successful find automatically increases the reference counter:
* it's up to the caller to decrease it again when done */
janus_refcount_increase(&session->ref);
}
janus_mutex_unlock(&sessions_mutex);
return session;
}
void janus_session_notify_event(janus_session *session, json_t *event) {
if(session != NULL && !g_atomic_int_get(&session->destroyed)) {
janus_request *source = janus_session_get_request(session);
if(source != NULL && source->transport != NULL) {
/* Send this to the transport client */
JANUS_LOG(LOG_HUGE, "Sending event to %s (%p)\n", source->transport->get_package(), source->instance);
source->transport->send_message(source->instance, NULL, FALSE, event);
} else {
/* No transport, free the event */
json_decref(event);
}
janus_request_unref(source);
} else {
/* No session, free the event */
json_decref(event);
}
}
/* Destroys a session but does not remove it from the sessions hash table. */
gint janus_session_destroy(janus_session *session) {
guint64 session_id = session->session_id;
JANUS_LOG(LOG_INFO, "Destroying session %"SCNu64"; %p\n", session_id, session);
if(!g_atomic_int_compare_and_exchange(&session->destroyed, 0, 1))
return 0;
janus_session_handles_clear(session);
/* The session will actually be destroyed when the counter gets to 0 */
janus_refcount_decrease(&session->ref);
return 0;
}
janus_ice_handle *janus_session_handles_find(janus_session *session, guint64 handle_id) {
if(session == NULL)
return NULL;
janus_mutex_lock(&session->mutex);
janus_ice_handle *handle = session->ice_handles ? g_hash_table_lookup(session->ice_handles, &handle_id) : NULL;
if(handle != NULL) {
/* A successful find automatically increases the reference counter:
* it's up to the caller to decrease it again when done */
janus_refcount_increase(&handle->ref);
}
janus_mutex_unlock(&session->mutex);
return handle;
}
void janus_session_handles_insert(janus_session *session, janus_ice_handle *handle) {
janus_mutex_lock(&session->mutex);
if(session->ice_handles == NULL)
session->ice_handles = g_hash_table_new_full(g_int64_hash, g_int64_equal, (GDestroyNotify)g_free, (GDestroyNotify)janus_ice_handle_dereference);
janus_refcount_increase(&handle->ref);
g_hash_table_insert(session->ice_handles, janus_uint64_dup(handle->handle_id), handle);
janus_mutex_unlock(&session->mutex);
}
gint janus_session_handles_remove(janus_session *session, janus_ice_handle *handle) {
janus_mutex_lock(&session->mutex);
gint error = janus_ice_handle_destroy(session, handle);
g_hash_table_remove(session->ice_handles, &handle->handle_id);
janus_mutex_unlock(&session->mutex);
return error;
}
void janus_session_handles_clear(janus_session *session) {
janus_mutex_lock(&session->mutex);
if(session->ice_handles != NULL && g_hash_table_size(session->ice_handles) > 0) {
GHashTableIter iter;
gpointer value;
/* Remove all handles */
g_hash_table_iter_init(&iter, session->ice_handles);
while (g_hash_table_iter_next(&iter, NULL, &value)) {
janus_ice_handle *handle = value;
if(!handle)
continue;
janus_ice_handle_destroy(session, handle);
g_hash_table_iter_remove(&iter);
}
}
janus_mutex_unlock(&session->mutex);
}
json_t *janus_session_handles_list_json(janus_session *session) {
json_t *list = json_array();
janus_mutex_lock(&session->mutex);
if(session->ice_handles != NULL && g_hash_table_size(session->ice_handles) > 0) {
GHashTableIter iter;
gpointer value;
g_hash_table_iter_init(&iter, session->ice_handles);
while (g_hash_table_iter_next(&iter, NULL, &value)) {
janus_ice_handle *handle = value;
if(!handle)
continue;
json_array_append_new(list, json_integer(handle->handle_id));
}
}
janus_mutex_unlock(&session->mutex);
return list;
}
/* Requests management */
static void janus_request_free(const janus_refcount *request_ref) {
janus_request *request = janus_refcount_containerof(request_ref, janus_request, ref);
/* This request can be destroyed, free all the resources */
request->transport = NULL;
if(request->instance)
janus_refcount_decrease(&request->instance->ref);
request->instance = NULL;
request->request_id = NULL;
if(request->message)
json_decref(request->message);
request->message = NULL;
g_free(request);
}
janus_request *janus_request_new(janus_transport *transport, janus_transport_session *instance, void *request_id, gboolean admin, json_t *message) {
janus_request *request = g_malloc(sizeof(janus_request));
request->transport = transport;
request->instance = instance;
janus_refcount_increase(&instance->ref);
request->request_id = request_id;
request->admin = admin;
request->message = message;
g_atomic_int_set(&request->destroyed, 0);
janus_refcount_init(&request->ref, janus_request_free);
return request;
}
void janus_request_destroy(janus_request *request) {
if(request == NULL || request == &exit_message || !g_atomic_int_compare_and_exchange(&request->destroyed, 0, 1))
return;
janus_refcount_decrease(&request->ref);
}
static int janus_request_check_secret(janus_request *request, guint64 session_id, const gchar *transaction_text) {
gboolean secret_authorized = FALSE, token_authorized = FALSE;
if(api_secret == NULL && !janus_auth_is_enabled()) {
/* Nothing to check */
secret_authorized = TRUE;
token_authorized = TRUE;
} else {
json_t *root = request->message;
if(api_secret != NULL) {
/* There's an API secret, check that the client provided it */
json_t *secret = json_object_get(root, "apisecret");
if(secret && json_is_string(secret) && janus_strcmp_const_time(json_string_value(secret), api_secret)) {
secret_authorized = TRUE;
}
}
if(janus_auth_is_enabled()) {
/* The token based authentication mechanism is enabled, check that the client provided it */
json_t *token = json_object_get(root, "token");
if(token && json_is_string(token) && janus_auth_check_token(json_string_value(token))) {
token_authorized = TRUE;
}
}
/* We consider a request authorized if either the proper API secret or a valid token has been provided */
if(!secret_authorized && !token_authorized)
return JANUS_ERROR_UNAUTHORIZED;
}
return 0;
}
static void janus_request_ice_handle_answer(janus_ice_handle *handle, int audio, int video, int data, char *jsep_sdp) {
/* We got our answer */
janus_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_PROCESSING_OFFER);
/* Any pending trickles? */
if(handle->pending_trickles) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] -- Processing %d pending trickle candidates\n", handle->handle_id, g_list_length(handle->pending_trickles));
GList *temp = NULL;
while(handle->pending_trickles) {
temp = g_list_first(handle->pending_trickles);
handle->pending_trickles = g_list_remove_link(handle->pending_trickles, temp);
janus_ice_trickle *trickle = (janus_ice_trickle *)temp->data;
g_list_free(temp);
if(trickle == NULL)
continue;
if((janus_get_monotonic_time() - trickle->received) > candidates_timeout*G_USEC_PER_SEC) {
/* FIXME Candidate is too old, discard it */
JANUS_LOG(LOG_WARN, "[%"SCNu64"] Discarding candidate (too old)\n", handle->handle_id);
janus_ice_trickle_destroy(trickle);
/* FIXME We should report that */
continue;
}
json_t *candidate = trickle->candidate;
if(candidate == NULL) {
janus_ice_trickle_destroy(trickle);
continue;
}
if(json_is_object(candidate)) {
/* We got a single candidate */
int error = 0;
const char *error_string = NULL;
if((error = janus_ice_trickle_parse(handle, candidate, &error_string)) != 0) {
/* FIXME We should report the error parsing the trickle candidate */
}
} else if(json_is_array(candidate)) {
/* We got multiple candidates in an array */
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Got multiple candidates (%zu)\n", handle->handle_id, json_array_size(candidate));
if(json_array_size(candidate) > 0) {
/* Handle remote candidates */
size_t i = 0;
for(i=0; i<json_array_size(candidate); i++) {
json_t *c = json_array_get(candidate, i);
/* FIXME We don't care if any trickle fails to parse */
janus_ice_trickle_parse(handle, c, NULL);
}
}
}
/* Done, free candidate */
janus_ice_trickle_destroy(trickle);
}
}
gboolean candidates_found = (handle->stream && handle->stream->component && g_slist_length(handle->stream->component->candidates) > 0);
/* This was an answer, check if it's time to start ICE */
if(janus_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_TRICKLE) && !candidates_found) {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] -- ICE Trickling is supported by the browser, waiting for remote candidates...\n", handle->handle_id);
janus_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_START);
} else {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Done! Sending connectivity checks...\n", handle->handle_id);
janus_ice_setup_remote_candidates(handle, handle->stream_id, 1);
}
}
int janus_process_incoming_request(janus_request *request) {
int ret = -1;
if(request == NULL) {
JANUS_LOG(LOG_ERR, "Missing request or payload to process, giving up...\n");
return ret;
}
int error_code = 0;
char error_cause[100];
json_t *root = request->message;
/* Ok, let's start with the ids */
guint64 session_id = 0, handle_id = 0;
json_t *s = json_object_get(root, "session_id");
if(json_is_null(s))
s = NULL;
if(s && json_is_integer(s))