-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
janus_nosip.c
2653 lines (2549 loc) · 105 KB
/
janus_nosip.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_nosip.c
* \author Lorenzo Miniero <[email protected]>
* \copyright GNU General Public License v3
* \brief Janus NoSIP plugin
* \details Check the \ref nosip for more details.
*
* \ingroup plugins
* \ref plugins
*
* \page nosip NoSIP plugin documentation
*
* This is quite a basic plugin, as it only takes care of acting as an
* RTP bridge. It is named "NoSIP" since, as the name suggests, signalling
* takes no place here, and is entirely up to the application. The typical
* usage of this application is something like this:
*
* 1. a WebRTC application handles signalling on its own (e.g., SIP), but
* needs to interact with a peer that doesn't support WebRTC (DTLS/ICE);
* 2. it creates a handle with the NoSIP plugin, creates a JSEP SDP offer,
* and passes it to the plugin;
* 3. the plugin creates a barebone SDP that can be used to communicate
* with the legacy peer, binds to the ports for RTP/RTCP, and sends this
* plain SDP back to the application;
* 4. the application uses this barebone SDP in its signalling, and expects
* an answer from the peer;
* 5. the SDP answer from the peer will be barebone as well, and so unfit
* for WebRTC usage; as such, the application passes it to the plugin as
* the answer to match the offer created before;
* 6. the plugin matches the answer to the offer, and starts exchanging
* RTP/RTCP with the legacy peer: media coming from the peer is relayed
* via WebRTC to the application, and WebRTC stuff coming from the application
* is relayed via plain RTP/RTCP to the legacy peer.
*
* The same behaviour can be followed if the application is the callee
* instead, with the only difference being that the barebone offer will
* come from the peer in this case, and the application will ask the
* NoSIP plugin for a barebone answer instead.
*
* As you can see, the behaviour is pretty much the same as the SIP plugin,
* with the key difference being that in this case there's no SIP stack in
* the plugin itself. All signalling is left to the application, and Janus
* (via the NoSIP plugin) is only responsible for bridging the media. This
* might be more appropriate than the SIP plugin in cases where developers
* want to keep control on the signalling layer, while still involving a
* server of sorts. Of course, SIP is just an example here: other signalling
* protocols may be involved as well (e.g., IAX, XMPP, others). The NoSIP
* plugin, though, will generate and expect plain SDP, so you'll need to
* take care of any adaptation that may be needed to make this work with
* the signalling protocol of your choice.
*
* \section nosipapi NoSIP Plugin API
*
* The plugin mainly supports two requests, \c generate and \c process,
* which are both asynchronous. The \c generate request take a JSEP offer
* or answer, and generates a barebone SDP the "legacy" application can
* use; the \c process request, on the other hand, processes a remote
* barebone SDP, and matches it to the plugin may have generated before,
* in order to then return a JSEP offer or answer that can be used to
* setup a PeerConnection.
*
* The \c generate request must be formatted as follows:
*
\verbatim
{
"request" : "generate",
"info" : "<opaque string that the user can provide for context; optional>",
"srtp" : "<whether to mandate (sdes_mandatory) or offer (sdes_optional) SRTP support; optional>",
"srtp_profile" : "<SRTP profile to negotiate, in case SRTP is offered; optional>"
}
\endverbatim
*
* As anticipated, this requires a JSEP offer or answer passed via Janus
* API as part of a WebRTC PeerConnection negotiation. If the conversion
* of the WebRTC JSEP SDP to barebone SDP is successful, a \c generated
* event is sent back to the user:
*
\verbatim
{
"event" : "generated",
"type" : "<offer|answer, depending on the nature of the provided JSEP>",
"sdp" : "<barebone SDP content>"
}
\endverbatim
*
* The \c process request, instead, must be formatted as follows:
*
\verbatim
{
"request" : "process",
"type" : "<offer|answer, depending on the nature of the provided SDP>",
"sdp" : "<barebone SDP to convert>"
"info" : "<opaque string that the user can provide for context; optional>",
"srtp" : "<whether to mandate (sdes_mandatory) or offer (sdes_optional) SRTP support; optional>",
"srtp_profile" : "<SRTP profile to negotiate, in case SRTP is offered; optional>"
}
\endverbatim
*
* As anticipated, this requires a "legacy" SDP offer or answer passed via
* NoSIP plugin messaging, which is why the caller must specify if it's an
* offer or answer. If the request is successful, a \c processed event is
* sent back to the user, along to the JSEP offer or answer that Janus
* generated out of the barebone SDP:
*
\verbatim
{
"event" : "processed",
"srtp" : "<whether the barebone SDP mandates (sdes_mandatory) or offers (sdes_optional) SRTP support; optional>"
}
\endverbatim
*
* To close a session you can use the \c hangup request, which needs no
* additional arguments, as the whole context can be extracted from the
* current state of the session in the plugin:
*
\verbatim
{
"request" : "hangup"
}
\endverbatim
*
* An \c hangingup event will be sent back, as this is an asynchronous request.
*
* Finally, just as in the SIP and SIPre plugins, the multimedia session
* can be recorded. Considering the NoSIP plugin also assumes two peers
* are in a call with each other (although it makes no assumptions on
* the signalling that ties them together), it works exactly the same
* way as the SIP and SIPre plugin do when it comes to recording.
* Specifically, you make use of the \c recording request to either start
* or stop a recording, using the following syntax:
*
\verbatim
{
"request" : "recording",
"action" : "<start|stop, depending on whether you want to start or stop recording something>"
"audio" : <true|false; whether or not our audio should be recorded>,
"video" : <true|false; whether or not our video should be recorded>,
"peer_audio" : <true|false; whether or not our peer's audio should be recorded>,
"peer_video" : <true|false; whether or not our peer's video should be recorded>,
"filename" : "<base path/filename to use for all the recordings>"
}
\endverbatim
*
* As you can see, this means that the two sides of conversation are recorded
* separately, and so are the audio and video streams if available. You can
* choose which ones to record, in case you're interested in just a subset.
* The \c filename part is just a prefix, and dictates the actual filenames
* that will be used for the up-to-four recordings that may need to be enabled.
*
* A \c recordingupdated event is sent back in case the request is successful.
*/
#include "plugin.h"
#include <arpa/inet.h>
#include <net/if.h>
#include <sys/socket.h>
#include <netdb.h>
#include <poll.h>
#include <jansson.h>
#include "../debug.h"
#include "../apierror.h"
#include "../config.h"
#include "../mutex.h"
#include "../record.h"
#include "../rtp.h"
#include "../rtpsrtp.h"
#include "../rtcp.h"
#include "../ip-utils.h"
#include "../sdp-utils.h"
#include "../utils.h"
/* Plugin information */
#define JANUS_NOSIP_VERSION 1
#define JANUS_NOSIP_VERSION_STRING "0.0.1"
#define JANUS_NOSIP_DESCRIPTION "This is a simple RTP bridging plugin that leaves signalling details (e.g., SIP) up to the application."
#define JANUS_NOSIP_NAME "JANUS NoSIP plugin"
#define JANUS_NOSIP_AUTHOR "Meetecho s.r.l."
#define JANUS_NOSIP_PACKAGE "janus.plugin.nosip"
/* Plugin methods */
janus_plugin *create(void);
int janus_nosip_init(janus_callbacks *callback, const char *config_path);
void janus_nosip_destroy(void);
int janus_nosip_get_api_compatibility(void);
int janus_nosip_get_version(void);
const char *janus_nosip_get_version_string(void);
const char *janus_nosip_get_description(void);
const char *janus_nosip_get_name(void);
const char *janus_nosip_get_author(void);
const char *janus_nosip_get_package(void);
void janus_nosip_create_session(janus_plugin_session *handle, int *error);
struct janus_plugin_result *janus_nosip_handle_message(janus_plugin_session *handle, char *transaction, json_t *message, json_t *jsep);
void janus_nosip_setup_media(janus_plugin_session *handle);
void janus_nosip_incoming_rtp(janus_plugin_session *handle, janus_plugin_rtp *packet);
void janus_nosip_incoming_rtcp(janus_plugin_session *handle, janus_plugin_rtcp *packet);
void janus_nosip_hangup_media(janus_plugin_session *handle);
void janus_nosip_destroy_session(janus_plugin_session *handle, int *error);
json_t *janus_nosip_query_session(janus_plugin_session *handle);
/* Plugin setup */
static janus_plugin janus_nosip_plugin =
JANUS_PLUGIN_INIT (
.init = janus_nosip_init,
.destroy = janus_nosip_destroy,
.get_api_compatibility = janus_nosip_get_api_compatibility,
.get_version = janus_nosip_get_version,
.get_version_string = janus_nosip_get_version_string,
.get_description = janus_nosip_get_description,
.get_name = janus_nosip_get_name,
.get_author = janus_nosip_get_author,
.get_package = janus_nosip_get_package,
.create_session = janus_nosip_create_session,
.handle_message = janus_nosip_handle_message,
.setup_media = janus_nosip_setup_media,
.incoming_rtp = janus_nosip_incoming_rtp,
.incoming_rtcp = janus_nosip_incoming_rtcp,
.hangup_media = janus_nosip_hangup_media,
.destroy_session = janus_nosip_destroy_session,
.query_session = janus_nosip_query_session,
);
/* Plugin creator */
janus_plugin *create(void) {
JANUS_LOG(LOG_VERB, "%s created!\n", JANUS_NOSIP_NAME);
return &janus_nosip_plugin;
}
/* Parameter validation */
static struct janus_json_parameter request_parameters[] = {
{"request", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter generate_parameters[] = {
{"info", JSON_STRING, 0},
{"srtp", JSON_STRING, 0},
{"srtp_profile", JSON_STRING, 0},
{"update", JANUS_JSON_BOOL, 0}
};
static struct janus_json_parameter process_parameters[] = {
{"type", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"sdp", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"info", JSON_STRING, 0},
{"srtp", JSON_STRING, 0},
{"srtp_profile", JSON_STRING, 0},
{"update", JANUS_JSON_BOOL, 0}
};
static struct janus_json_parameter recording_parameters[] = {
{"action", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"audio", JANUS_JSON_BOOL, 0},
{"video", JANUS_JSON_BOOL, 0},
{"peer_audio", JANUS_JSON_BOOL, 0},
{"peer_video", JANUS_JSON_BOOL, 0},
{"filename", JSON_STRING, 0}
};
/* Useful stuff */
static volatile gint initialized = 0, stopping = 0;
static gboolean notify_events = TRUE;
static gboolean ipv6_disabled = FALSE;
static janus_callbacks *gateway = NULL;
static char *local_ip = NULL, *sdp_ip = NULL;
static janus_network_address janus_network_local_ip = { 0 };
#define DEFAULT_RTP_RANGE_MIN 10000
#define DEFAULT_RTP_RANGE_MAX 60000
static uint16_t rtp_range_min = DEFAULT_RTP_RANGE_MIN;
static uint16_t rtp_range_max = DEFAULT_RTP_RANGE_MAX;
static uint16_t rtp_range_slider = DEFAULT_RTP_RANGE_MIN;
static int dscp_audio_rtp = 0;
static int dscp_video_rtp = 0;
static GThread *handler_thread;
static void *janus_nosip_handler(void *data);
static void janus_nosip_hangup_media_internal(janus_plugin_session *handle);
typedef struct janus_nosip_message {
janus_plugin_session *handle;
char *transaction;
json_t *message;
json_t *jsep;
} janus_nosip_message;
static GAsyncQueue *messages = NULL;
static janus_nosip_message exit_message;
typedef struct janus_nosip_media {
char *remote_audio_ip;
char *remote_video_ip;
gboolean ready;
gboolean require_srtp, has_srtp_local, has_srtp_remote;
janus_srtp_profile srtp_profile;
gboolean has_audio;
int audio_rtp_fd, audio_rtcp_fd;
int local_audio_rtp_port, remote_audio_rtp_port;
int local_audio_rtcp_port, remote_audio_rtcp_port;
guint32 audio_ssrc, audio_ssrc_peer;
int audio_pt, opusred_pt;
const char *audio_pt_name;
gint32 audio_srtp_tag;
srtp_t audio_srtp_in, audio_srtp_out;
srtp_policy_t audio_remote_policy, audio_local_policy;
char *audio_srtp_local_profile, *audio_srtp_local_crypto;
gboolean audio_send;
gboolean has_video;
int video_rtp_fd, video_rtcp_fd;
int local_video_rtp_port, remote_video_rtp_port;
int local_video_rtcp_port, remote_video_rtcp_port;
guint32 video_ssrc, video_ssrc_peer;
guint32 simulcast_ssrc;
int video_pt;
const char *video_pt_name;
gint32 video_srtp_tag;
srtp_t video_srtp_in, video_srtp_out;
srtp_policy_t video_remote_policy, video_local_policy;
char *video_srtp_local_profile, *video_srtp_local_crypto;
gboolean video_send;
janus_rtp_switching_context acontext, vcontext;
int pipefd[2];
gboolean updated;
int video_orientation_extension_id;
int audio_level_extension_id;
} janus_nosip_media;
typedef struct janus_nosip_session {
janus_plugin_session *handle;
gint64 sdp_version;
janus_nosip_media media; /* Media gatewaying stuff (same stuff as the SIP plugin) */
janus_sdp *sdp; /* The SDP this user sent */
janus_recorder *arc; /* The Janus recorder instance for this user's audio, if enabled */
janus_recorder *arc_peer; /* The Janus recorder instance for the peer's audio, if enabled */
janus_recorder *vrc; /* The Janus recorder instance for this user's video, if enabled */
janus_recorder *vrc_peer; /* The Janus recorder instance for the peer's video, if enabled */
janus_mutex rec_mutex; /* Mutex to protect the recorders from race conditions */
GThread *relayer_thread;
volatile gint hangingup;
volatile gint destroyed;
janus_refcount ref;
janus_mutex mutex;
} janus_nosip_session;
static GHashTable *sessions;
static janus_mutex sessions_mutex = JANUS_MUTEX_INITIALIZER;
static void janus_nosip_srtp_cleanup(janus_nosip_session *session);
static void janus_nosip_media_reset(janus_nosip_session *session);
static void janus_nosip_session_destroy(janus_nosip_session *session) {
if(session && g_atomic_int_compare_and_exchange(&session->destroyed, 0, 1))
janus_refcount_decrease(&session->ref);
}
static void janus_nosip_session_free(const janus_refcount *session_ref) {
janus_nosip_session *session = janus_refcount_containerof(session_ref, janus_nosip_session, ref);
/* Remove the reference to the core plugin session */
janus_refcount_decrease(&session->handle->ref);
/* This session can be destroyed, free all the resources */
janus_sdp_destroy(session->sdp);
session->sdp = NULL;
g_free(session->media.remote_audio_ip);
session->media.remote_audio_ip = NULL;
g_free(session->media.remote_video_ip);
session->media.remote_video_ip = NULL;
janus_nosip_srtp_cleanup(session);
session->handle = NULL;
g_free(session);
session = NULL;
}
static void janus_nosip_message_free(janus_nosip_message *msg) {
if(!msg || msg == &exit_message)
return;
if(msg->handle && msg->handle->plugin_handle) {
janus_nosip_session *session = (janus_nosip_session *)msg->handle->plugin_handle;
janus_refcount_decrease(&session->ref);
}
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);
}
/* SRTP stuff (in case we need SDES) */
static int janus_nosip_srtp_set_local(janus_nosip_session *session, gboolean video, char **profile, char **crypto) {
if(session == NULL)
return -1;
/* Which SRTP profile are we going to negotiate? */
int key_length = 0, salt_length = 0, master_length = 0;
if(session->media.srtp_profile == JANUS_SRTP_AES128_CM_SHA1_32) {
key_length = SRTP_MASTER_KEY_LENGTH;
salt_length = SRTP_MASTER_SALT_LENGTH;
master_length = SRTP_MASTER_LENGTH;
*profile = g_strdup("AES_CM_128_HMAC_SHA1_32");
} else if(session->media.srtp_profile == JANUS_SRTP_AES128_CM_SHA1_80) {
key_length = SRTP_MASTER_KEY_LENGTH;
salt_length = SRTP_MASTER_SALT_LENGTH;
master_length = SRTP_MASTER_LENGTH;
*profile = g_strdup("AES_CM_128_HMAC_SHA1_80");
#ifdef HAVE_SRTP_AESGCM
} else if(session->media.srtp_profile == JANUS_SRTP_AEAD_AES_128_GCM) {
key_length = SRTP_AESGCM128_MASTER_KEY_LENGTH;
salt_length = SRTP_AESGCM128_MASTER_SALT_LENGTH;
master_length = SRTP_AESGCM128_MASTER_LENGTH;
*profile = g_strdup("AEAD_AES_128_GCM");
} else if(session->media.srtp_profile == JANUS_SRTP_AEAD_AES_256_GCM) {
key_length = SRTP_AESGCM256_MASTER_KEY_LENGTH;
salt_length = SRTP_AESGCM256_MASTER_SALT_LENGTH;
master_length = SRTP_AESGCM256_MASTER_LENGTH;
*profile = g_strdup("AEAD_AES_256_GCM");
#endif
} else {
JANUS_LOG(LOG_ERR, "[NoSIP-%p] Unsupported SRTP profile\n", session);
return -2;
}
JANUS_LOG(LOG_WARN, "[NoSIP-%p] %s\n", session, *profile);
JANUS_LOG(LOG_WARN, "[NoSIP-%p] Key/Salt/Master: %d/%d/%d\n",
session, master_length, key_length, salt_length);
/* Generate key/salt */
uint8_t *key = g_malloc0(master_length);
srtp_crypto_get_random(key, master_length);
/* Set SRTP policies */
srtp_policy_t *policy = video ? &session->media.video_local_policy : &session->media.audio_local_policy;
switch(session->media.srtp_profile) {
case JANUS_SRTP_AES128_CM_SHA1_32:
srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(&(policy->rtp));
srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&(policy->rtcp));
break;
case JANUS_SRTP_AES128_CM_SHA1_80:
srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&(policy->rtp));
srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&(policy->rtcp));
break;
#ifdef HAVE_SRTP_AESGCM
case JANUS_SRTP_AEAD_AES_128_GCM:
srtp_crypto_policy_set_aes_gcm_128_16_auth(&(policy->rtp));
srtp_crypto_policy_set_aes_gcm_128_16_auth(&(policy->rtcp));
break;
case JANUS_SRTP_AEAD_AES_256_GCM:
srtp_crypto_policy_set_aes_gcm_256_16_auth(&(policy->rtp));
srtp_crypto_policy_set_aes_gcm_256_16_auth(&(policy->rtcp));
break;
#endif
default:
/* Will never happen? */
JANUS_LOG(LOG_WARN, "[NoSIP-%p] Unsupported SRTP profile\n", session);
break;
}
policy->ssrc.type = ssrc_any_inbound;
policy->key = key;
policy->next = NULL;
/* Create SRTP context */
srtp_err_status_t res = srtp_create(video ? &session->media.video_srtp_out : &session->media.audio_srtp_out, policy);
if(res != srtp_err_status_ok) {
/* Something went wrong... */
JANUS_LOG(LOG_ERR, "Oops, error creating outbound SRTP session: %d (%s)\n", res, janus_srtp_error_str(res));
g_free(*profile);
*profile = NULL;
g_free(key);
policy->key = NULL;
return -2;
}
/* Base64 encode the salt */
*crypto = g_base64_encode(key, master_length);
if((video && session->media.video_srtp_out) || (!video && session->media.audio_srtp_out)) {
JANUS_LOG(LOG_VERB, "%s outbound SRTP session created\n", video ? "Video" : "Audio");
}
return 0;
}
static int janus_nosip_srtp_set_remote(janus_nosip_session *session, gboolean video, const char *profile, const char *crypto) {
if(session == NULL || profile == NULL || crypto == NULL)
return -1;
/* Which SRTP profile is being negotiated? */
JANUS_LOG(LOG_WARN, "[NoSIP-%p] %s\n", session, profile);
gsize key_length = 0, salt_length = 0, master_length = 0;
if(!strcasecmp(profile, "AES_CM_128_HMAC_SHA1_32")) {
session->media.srtp_profile = JANUS_SRTP_AES128_CM_SHA1_32;
key_length = SRTP_MASTER_KEY_LENGTH;
salt_length = SRTP_MASTER_SALT_LENGTH;
master_length = SRTP_MASTER_LENGTH;
} else if(!strcasecmp(profile, "AES_CM_128_HMAC_SHA1_80")) {
session->media.srtp_profile = JANUS_SRTP_AES128_CM_SHA1_80;
key_length = SRTP_MASTER_KEY_LENGTH;
salt_length = SRTP_MASTER_SALT_LENGTH;
master_length = SRTP_MASTER_LENGTH;
#ifdef HAVE_SRTP_AESGCM
} else if(!strcasecmp(profile, "AEAD_AES_128_GCM")) {
session->media.srtp_profile = JANUS_SRTP_AEAD_AES_128_GCM;
key_length = SRTP_AESGCM128_MASTER_KEY_LENGTH;
salt_length = SRTP_AESGCM128_MASTER_SALT_LENGTH;
master_length = SRTP_AESGCM128_MASTER_LENGTH;
} else if(!strcasecmp(profile, "AEAD_AES_256_GCM")) {
session->media.srtp_profile = JANUS_SRTP_AEAD_AES_256_GCM;
key_length = SRTP_AESGCM256_MASTER_KEY_LENGTH;
salt_length = SRTP_AESGCM256_MASTER_SALT_LENGTH;
master_length = SRTP_AESGCM256_MASTER_LENGTH;
#endif
} else {
JANUS_LOG(LOG_WARN, "[NoSIP-%p] Unsupported SRTP profile %s\n", session, profile);
return -2;
}
JANUS_LOG(LOG_VERB, "[NoSIP-%p] Key/Salt/Master: %zu/%zu/%zu\n",
session, master_length, key_length, salt_length);
/* Base64 decode the crypto string and set it as the remote SRTP context */
gsize len = 0;
guchar *decoded = g_base64_decode(crypto, &len);
if(len < master_length) {
/* FIXME Can this happen? */
g_free(decoded);
return -3;
}
/* Set SRTP policies */
srtp_policy_t *policy = video ? &session->media.video_remote_policy : &session->media.audio_remote_policy;
switch(session->media.srtp_profile) {
case JANUS_SRTP_AES128_CM_SHA1_32:
srtp_crypto_policy_set_aes_cm_128_hmac_sha1_32(&(policy->rtp));
srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&(policy->rtcp));
break;
case JANUS_SRTP_AES128_CM_SHA1_80:
srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&(policy->rtp));
srtp_crypto_policy_set_aes_cm_128_hmac_sha1_80(&(policy->rtcp));
break;
#ifdef HAVE_SRTP_AESGCM
case JANUS_SRTP_AEAD_AES_128_GCM:
srtp_crypto_policy_set_aes_gcm_128_16_auth(&(policy->rtp));
srtp_crypto_policy_set_aes_gcm_128_16_auth(&(policy->rtcp));
break;
case JANUS_SRTP_AEAD_AES_256_GCM:
srtp_crypto_policy_set_aes_gcm_256_16_auth(&(policy->rtp));
srtp_crypto_policy_set_aes_gcm_256_16_auth(&(policy->rtcp));
break;
#endif
default:
/* Will never happen? */
JANUS_LOG(LOG_WARN, "[NoSIP-%p] Unsupported SRTP profile\n", session);
break;
}
policy->ssrc.type = ssrc_any_inbound;
policy->key = decoded;
policy->next = NULL;
/* Create SRTP context */
srtp_err_status_t res = srtp_create(video ? &session->media.video_srtp_in : &session->media.audio_srtp_in, policy);
if(res != srtp_err_status_ok) {
/* Something went wrong... */
JANUS_LOG(LOG_ERR, "Oops, error creating inbound SRTP session: %d (%s)\n", res, janus_srtp_error_str(res));
g_free(decoded);
policy->key = NULL;
return -2;
}
if((video && session->media.video_srtp_in) || (!video && session->media.audio_srtp_in)) {
JANUS_LOG(LOG_VERB, "%s inbound SRTP session created\n", video ? "Video" : "Audio");
}
return 0;
}
static void janus_nosip_srtp_cleanup(janus_nosip_session *session) {
if(session == NULL)
return;
session->media.require_srtp = FALSE;
session->media.has_srtp_local = FALSE;
session->media.has_srtp_remote = FALSE;
session->media.srtp_profile = 0;
/* Audio */
session->media.audio_srtp_tag = 0;
if(session->media.audio_srtp_out)
srtp_dealloc(session->media.audio_srtp_out);
session->media.audio_srtp_out = NULL;
g_free(session->media.audio_local_policy.key);
session->media.audio_local_policy.key = NULL;
if(session->media.audio_srtp_in)
srtp_dealloc(session->media.audio_srtp_in);
session->media.audio_srtp_in = NULL;
g_free(session->media.audio_remote_policy.key);
session->media.audio_remote_policy.key = NULL;
if(session->media.audio_srtp_local_profile) {
g_free(session->media.audio_srtp_local_profile);
session->media.audio_srtp_local_profile = NULL;
}
if(session->media.audio_srtp_local_crypto) {
g_free(session->media.audio_srtp_local_crypto);
session->media.audio_srtp_local_crypto = NULL;
}
/* Video */
session->media.video_srtp_tag = 0;
if(session->media.video_srtp_out)
srtp_dealloc(session->media.video_srtp_out);
session->media.video_srtp_out = NULL;
g_free(session->media.video_local_policy.key);
session->media.video_local_policy.key = NULL;
if(session->media.video_srtp_in)
srtp_dealloc(session->media.video_srtp_in);
session->media.video_srtp_in = NULL;
g_free(session->media.video_remote_policy.key);
session->media.video_remote_policy.key = NULL;
if(session->media.video_srtp_local_profile) {
g_free(session->media.video_srtp_local_profile);
session->media.video_srtp_local_profile = NULL;
}
if(session->media.video_srtp_local_crypto) {
g_free(session->media.video_srtp_local_crypto);
session->media.video_srtp_local_crypto = NULL;
}
}
void janus_nosip_media_reset(janus_nosip_session *session) {
if(session == NULL)
return;
g_free(session->media.remote_audio_ip);
session->media.remote_audio_ip = NULL;
g_free(session->media.remote_video_ip);
session->media.remote_video_ip = NULL;
session->media.updated = FALSE;
session->media.ready = FALSE;
session->media.require_srtp = FALSE;
session->media.has_audio = FALSE;
session->media.audio_pt = -1;
session->media.opusred_pt = -1;
session->media.audio_pt_name = NULL; /* Immutable string, no need to free*/
session->media.audio_send = TRUE;
session->media.has_video = FALSE;
session->media.video_pt = -1;
session->media.video_pt_name = NULL; /* Immutable string, no need to free*/
session->media.video_send = TRUE;
session->media.video_orientation_extension_id = -1;
session->media.audio_level_extension_id = -1;
janus_rtp_switching_context_reset(&session->media.acontext);
janus_rtp_switching_context_reset(&session->media.vcontext);
}
/* SDP parsing and manipulation */
void janus_nosip_sdp_process(janus_nosip_session *session, janus_sdp *sdp, gboolean answer, gboolean update, gboolean *changed);
char *janus_nosip_sdp_manipulate(janus_nosip_session *session, janus_sdp *sdp, gboolean answer);
/* Media */
static int janus_nosip_allocate_local_ports(janus_nosip_session *session, gboolean update);
static void *janus_nosip_relay_thread(void *data);
static void janus_nosip_media_cleanup(janus_nosip_session *session);
/* Error codes */
#define JANUS_NOSIP_ERROR_UNKNOWN_ERROR 499
#define JANUS_NOSIP_ERROR_NO_MESSAGE 440
#define JANUS_NOSIP_ERROR_INVALID_JSON 441
#define JANUS_NOSIP_ERROR_INVALID_REQUEST 442
#define JANUS_NOSIP_ERROR_MISSING_ELEMENT 443
#define JANUS_NOSIP_ERROR_INVALID_ELEMENT 444
#define JANUS_NOSIP_ERROR_WRONG_STATE 445
#define JANUS_NOSIP_ERROR_MISSING_SDP 446
#define JANUS_NOSIP_ERROR_INVALID_SDP 447
#define JANUS_NOSIP_ERROR_IO_ERROR 448
#define JANUS_NOSIP_ERROR_RECORDING_ERROR 449
#define JANUS_NOSIP_ERROR_TOO_STRICT 450
/* Plugin implementation */
int janus_nosip_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.jcfg", config_path, JANUS_NOSIP_PACKAGE);
JANUS_LOG(LOG_VERB, "Configuration file: %s\n", filename);
janus_config *config = janus_config_parse(filename);
if(config == NULL) {
JANUS_LOG(LOG_WARN, "Couldn't find .jcfg configuration file (%s), trying .cfg\n", JANUS_NOSIP_PACKAGE);
g_snprintf(filename, 255, "%s/%s.cfg", config_path, JANUS_NOSIP_PACKAGE);
JANUS_LOG(LOG_VERB, "Configuration file: %s\n", filename);
config = janus_config_parse(filename);
}
if(config != NULL) {
janus_config_print(config);
janus_config_category *config_general = janus_config_get_create(config, NULL, janus_config_type_category, "general");
janus_config_item *item = janus_config_get(config, config_general, janus_config_type_item, "local_ip");
if(item && item->value && strlen(item->value) > 0) {
/* Verify that the address is valid */
struct ifaddrs *ifas = NULL;
janus_network_address iface;
janus_network_address_string_buffer ibuf;
if(getifaddrs(&ifas) == -1) {
JANUS_LOG(LOG_ERR, "Unable to acquire list of network devices/interfaces; some configurations may not work as expected... %d (%s)\n",
errno, g_strerror(errno));
} else {
if(janus_network_lookup_interface(ifas, item->value, &iface) != 0) {
JANUS_LOG(LOG_WARN, "Error setting local IP address to %s, falling back to detecting IP address...\n", item->value);
} else {
if(janus_network_address_to_string_buffer(&iface, &ibuf) != 0 || janus_network_address_string_buffer_is_null(&ibuf)) {
JANUS_LOG(LOG_WARN, "Error getting local IP address from %s, falling back to detecting IP address...\n", item->value);
} else {
local_ip = g_strdup(janus_network_address_string_from_buffer(&ibuf));
}
}
freeifaddrs(ifas);
}
}
item = janus_config_get(config, config_general, janus_config_type_item, "sdp_ip");
if(item && item->value && strlen(item->value) > 0) {
sdp_ip = g_strdup(item->value);
JANUS_LOG(LOG_VERB, "IP to advertise in SDP: %s\n", sdp_ip);
}
/* Make sure both IPs are valid, if provided */
janus_network_address_nullify(&janus_network_local_ip);
if(local_ip) {
if(janus_network_string_to_address(janus_network_query_options_any_ip, local_ip, &janus_network_local_ip) != 0) {
JANUS_LOG(LOG_ERR, "Invalid local media IP address [%s]...\n", local_ip);
return -1;
}
if((janus_network_local_ip.family == AF_INET && janus_network_local_ip.ipv4.s_addr == INADDR_ANY) ||
(janus_network_local_ip.family == AF_INET6 && IN6_IS_ADDR_UNSPECIFIED(&janus_network_local_ip.ipv6))) {
janus_network_address_nullify(&janus_network_local_ip);
}
}
JANUS_LOG(LOG_VERB, "Binding media address set to [%s]...\n", janus_network_address_is_null(&janus_network_local_ip) ? "any" : local_ip);
if(!sdp_ip) {
char *ip = janus_network_address_is_null(&janus_network_local_ip) ? local_ip : NULL;
if(ip) {
sdp_ip = g_strdup(ip);
JANUS_LOG(LOG_VERB, "IP to advertise in SDP: %s\n", sdp_ip);
}
}
item = janus_config_get(config, config_general, janus_config_type_item, "rtp_port_range");
if(item && item->value) {
/* Split in min and max port */
char *maxport = strrchr(item->value, '-');
if(maxport != NULL) {
*maxport = '\0';
maxport++;
if(janus_string_to_uint16(item->value, &rtp_range_min) < 0)
JANUS_LOG(LOG_WARN, "Invalid RTP min port value: %s (assuming 0)\n", item->value);
if(janus_string_to_uint16(maxport, &rtp_range_max) < 0)
JANUS_LOG(LOG_WARN, "Invalid RTP max port value: %s (assuming 0)\n", maxport);
maxport--;
*maxport = '-';
}
if(rtp_range_min > rtp_range_max) {
uint16_t temp_port = rtp_range_min;
rtp_range_min = rtp_range_max;
rtp_range_max = temp_port;
}
if(rtp_range_min % 2)
rtp_range_min++; /* Pick an even port for RTP */
if(rtp_range_min > rtp_range_max) {
JANUS_LOG(LOG_WARN, "Incorrect port range (%u -- %u), switching min and max\n", rtp_range_min, rtp_range_max);
uint16_t range_temp = rtp_range_max;
rtp_range_max = rtp_range_min;
rtp_range_min = range_temp;
}
if(rtp_range_max == 0)
rtp_range_max = 65535;
rtp_range_slider = rtp_range_min;
JANUS_LOG(LOG_VERB, "NoSIP RTP/RTCP port range: %u -- %u\n", rtp_range_min, rtp_range_max);
}
item = janus_config_get(config, config_general, janus_config_type_item, "events");
if(item != NULL && item->value != NULL)
notify_events = janus_is_true(item->value);
if(!notify_events && callback->events_is_enabled()) {
JANUS_LOG(LOG_WARN, "Notification of events to handlers disabled for %s\n", JANUS_NOSIP_NAME);
}
/* Is there any DSCP TOS to apply? */
item = janus_config_get(config, config_general, janus_config_type_item, "dscp_audio_rtp");
if(item && item->value) {
int val = atoi(item->value);
if(val < 0) {
JANUS_LOG(LOG_WARN, "Ignoring dscp_audio_rtp value as it's not a positive integer\n");
} else {
dscp_audio_rtp = val;
}
}
item = janus_config_get(config, config_general, janus_config_type_item, "dscp_video_rtp");
if(item && item->value) {
int val = atoi(item->value);
if(val < 0) {
JANUS_LOG(LOG_WARN, "Ignoring dscp_video_rtp value as it's not a positive integer\n");
} else {
dscp_video_rtp = val;
}
}
janus_config_destroy(config);
}
config = NULL;
if(local_ip == NULL) {
local_ip = janus_network_detect_local_ip_as_string(janus_network_query_options_any_ip);
if(local_ip == NULL) {
JANUS_LOG(LOG_WARN, "Couldn't find any address! using 127.0.0.1 as the local IP... (which is NOT going to work out of your machine)\n");
local_ip = g_strdup("127.0.0.1");
}
}
JANUS_LOG(LOG_VERB, "Local IP set to %s\n", local_ip);
sessions = g_hash_table_new_full(NULL, NULL, NULL, (GDestroyNotify)janus_nosip_session_destroy);
messages = g_async_queue_new_full((GDestroyNotify) janus_nosip_message_free);
/* This is the callback we'll need to invoke to contact the Janus core */
gateway = callback;
if(janus_network_address_is_null(&janus_network_local_ip) ||
janus_network_local_ip.family == AF_INET6) {
/* Finally, let's check if IPv6 is disabled, as we may need to know for RTP/RTCP sockets */
int fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if(fd <= 0) {
ipv6_disabled = TRUE;
} else {
int v6only = 0;
if(setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)) != 0)
ipv6_disabled = TRUE;
}
if(fd > 0)
close(fd);
if(ipv6_disabled) {
if(!janus_network_address_is_null(&janus_network_local_ip)) {
JANUS_LOG(LOG_ERR, "IPv6 disabled and local media address is IPv6...\n");
return -1;
}
JANUS_LOG(LOG_WARN, "IPv6 disabled, will only use IPv4 for RTP/RTCP sockets (SIP)\n");
}
} else if(janus_network_local_ip.family == AF_INET) {
/* Disable if we have a specified IPv4 address for RTP/RTCP sockets */
ipv6_disabled = TRUE;
}
g_atomic_int_set(&initialized, 1);
GError *error = NULL;
/* Launch the thread that will handle incoming messages */
handler_thread = g_thread_try_new("nosip handler", janus_nosip_handler, NULL, &error);
if(error != NULL) {
g_atomic_int_set(&initialized, 0);
JANUS_LOG(LOG_ERR, "Got error %d (%s) trying to launch the NoSIP handler thread...\n",
error->code, error->message ? error->message : "??");
g_error_free(error);
return -1;
}
JANUS_LOG(LOG_INFO, "%s initialized!\n", JANUS_NOSIP_NAME);
return 0;
}
void janus_nosip_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;
}
/* FIXME We should destroy the sessions cleanly */
janus_mutex_lock(&sessions_mutex);
g_hash_table_destroy(sessions);
sessions = NULL;
janus_mutex_unlock(&sessions_mutex);
g_async_queue_unref(messages);
messages = NULL;
g_atomic_int_set(&initialized, 0);
g_atomic_int_set(&stopping, 0);
g_free(local_ip);
g_free(sdp_ip);
JANUS_LOG(LOG_INFO, "%s destroyed!\n", JANUS_NOSIP_NAME);
}
int janus_nosip_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_nosip_get_version(void) {
return JANUS_NOSIP_VERSION;
}
const char *janus_nosip_get_version_string(void) {
return JANUS_NOSIP_VERSION_STRING;
}
const char *janus_nosip_get_description(void) {
return JANUS_NOSIP_DESCRIPTION;
}
const char *janus_nosip_get_name(void) {
return JANUS_NOSIP_NAME;
}
const char *janus_nosip_get_author(void) {
return JANUS_NOSIP_AUTHOR;
}
const char *janus_nosip_get_package(void) {
return JANUS_NOSIP_PACKAGE;
}
static janus_nosip_session *janus_nosip_lookup_session(janus_plugin_session *handle) {
janus_nosip_session *session = NULL;
if (g_hash_table_contains(sessions, handle)) {
session = (janus_nosip_session *)handle->plugin_handle;
}
return session;
}
void janus_nosip_create_session(janus_plugin_session *handle, int *error) {
if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized)) {
*error = -1;
return;
}
janus_nosip_session *session = g_malloc0(sizeof(janus_nosip_session));
session->handle = handle;
session->sdp = NULL;
session->media.remote_audio_ip = NULL;
session->media.remote_video_ip = NULL;
session->media.ready = FALSE;
session->media.require_srtp = FALSE;
session->media.has_srtp_local = FALSE;
session->media.has_srtp_remote = FALSE;
session->media.srtp_profile = 0;
session->media.audio_srtp_local_profile = NULL;
session->media.audio_srtp_local_crypto = NULL;
session->media.video_srtp_local_profile = NULL;
session->media.video_srtp_local_crypto = NULL;
session->media.has_audio = FALSE;
session->media.audio_rtp_fd = -1;
session->media.audio_rtcp_fd = -1;
session->media.local_audio_rtp_port = 0;
session->media.remote_audio_rtp_port = 0;
session->media.local_audio_rtcp_port = 0;
session->media.remote_audio_rtcp_port = 0;
session->media.audio_ssrc = 0;
session->media.audio_ssrc_peer = 0;
session->media.audio_pt = -1;
session->media.opusred_pt = -1;
session->media.audio_pt_name = NULL;
session->media.audio_send = TRUE;
session->media.has_video = FALSE;
session->media.video_rtp_fd = -1;
session->media.video_rtcp_fd = -1;
session->media.local_video_rtp_port = 0;
session->media.remote_video_rtp_port = 0;
session->media.local_video_rtcp_port = 0;
session->media.remote_video_rtcp_port = 0;
session->media.video_ssrc = 0;
session->media.video_ssrc_peer = 0;
session->media.simulcast_ssrc = 0;
session->media.video_pt = -1;
session->media.video_pt_name = NULL;
session->media.video_send = TRUE;
session->media.video_orientation_extension_id = -1;
session->media.audio_level_extension_id = -1;
/* Initialize the RTP context */
janus_rtp_switching_context_reset(&session->media.acontext);
janus_rtp_switching_context_reset(&session->media.vcontext);
session->media.pipefd[0] = -1;
session->media.pipefd[1] = -1;
session->media.updated = FALSE;
session->media.audio_remote_policy.ssrc.type = ssrc_any_inbound;
session->media.audio_local_policy.ssrc.type = ssrc_any_inbound;
session->media.video_remote_policy.ssrc.type = ssrc_any_inbound;
session->media.video_local_policy.ssrc.type = ssrc_any_inbound;
janus_mutex_init(&session->rec_mutex);
g_atomic_int_set(&session->destroyed, 0);
g_atomic_int_set(&session->hangingup, 0);
janus_mutex_init(&session->mutex);
handle->plugin_handle = session;
janus_refcount_init(&session->ref, janus_nosip_session_free);
janus_mutex_lock(&sessions_mutex);
g_hash_table_insert(sessions, handle, session);
janus_mutex_unlock(&sessions_mutex);
return;
}
void janus_nosip_destroy_session(janus_plugin_session *handle, int *error) {
if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized)) {
*error = -1;
return;
}
janus_mutex_lock(&sessions_mutex);
janus_nosip_session *session = janus_nosip_lookup_session(handle);