-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
janus_streaming.c
4446 lines (4316 loc) · 175 KB
/
janus_streaming.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_streaming.c
* \author Lorenzo Miniero <[email protected]>
* \copyright GNU General Public License v3
* \brief Janus Streaming plugin
* \details This is a streaming plugin for Janus, allowing WebRTC peers
* to watch/listen to pre-recorded files or media generated by another tool.
* Specifically, the plugin currently supports three different type of streams:
*
* -# on-demand streaming of pre-recorded media files (different
* streaming context for each peer);
* -# live streaming of pre-recorded media files (shared streaming
* context for all peers attached to the stream);
* -# live streaming of media generated by another tool (shared
* streaming context for all peers attached to the stream).
*
* For what concerns types 1. and 2., considering the proof of concept
* nature of the implementation the only pre-recorded media files
* that the plugins supports right now are raw mu-Law and a-Law files:
* support is of course planned for other additional widespread formats
* as well.
*
* For what concerns type 3., instead, the plugin is configured
* to listen on a couple of ports for RTP: this means that the plugin
* is implemented to receive RTP on those ports and relay them to all
* peers attached to that stream. Any tool that can generate audio/video
* RTP streams and specify a destination is good for the purpose: the
* examples section contains samples that make use of GStreamer (http://gstreamer.freedesktop.org/)
* but other tools like FFmpeg (http://www.ffmpeg.org/), LibAV (http://libav.org/)
* or others are fine as well. This makes it really easy to capture and
* encode whatever you want using your favourite tool, and then have it
* transparently broadcasted via WebRTC using Janus. Notice that we recently
* added the possibility to also add a datachannel track to an RTP streaming
* mountpoint: this allows you to send, via UDP, a text-based message to
* relay via datachannels (e.g., the title of the current song, if this
* is a radio streaming channel). When using this feature, though, beware
* that you'll have to stay within the boundaries of the MTU, as each
* message will have to stay within the size of an UDP packet.
*
* Streams to make available are listed in the plugin configuration file.
* A pre-filled configuration file is provided in \c conf/janus.plugin.streaming.cfg
* and includes a stream of every type.
*
* To add more streams or modify the existing ones, you can use the following
* syntax:
*
* \verbatim
[stream-name]
type = rtp|live|ondemand|rtsp
rtp = stream originated by an external tool (e.g., gstreamer or
ffmpeg) and sent to the plugin via RTP
live = local file streamed live to multiple listeners
(multiple listeners = same streaming context)
ondemand = local file streamed on-demand to a single listener
(multiple listeners = different streaming contexts)
rtsp = stream originated by an external RTSP feed (only
available if libcurl support was compiled)
id = <unique numeric ID>
description = This is my awesome stream
is_private = yes|no (private streams don't appear when you do a 'list' request)
filename = path to the local file to stream (only for live/ondemand)
secret = <optional password needed for manipulating (e.g., destroying
or enabling/disabling) the stream>
pin = <optional password needed for watching the stream>
audio = yes|no (do/don't stream audio)
video = yes|no (do/don't stream video)
The following options are only valid for the 'rtp' type:
data = yes|no (do/don't stream text via datachannels)
audioport = local port for receiving audio frames
audiomcast = multicast group port for receiving audio frames, if any
audioiface = network interface or IP address to bind to, if any (binds to all otherwise)
audiopt = <audio RTP payload type> (e.g., 111)
audiortpmap = RTP map of the audio codec (e.g., opus/48000/2)
audiofmtp = Codec specific parameters, if any
videoport = local port for receiving video frames (only for rtp)
videomcast = multicast group port for receiving video frames, if any
videoiface = network interface or IP address to bind to, if any (binds to all otherwise)
videopt = <video RTP payload type> (e.g., 100)
videortpmap = RTP map of the video codec (e.g., VP8/90000)
videofmtp = Codec specific parameters, if any
videobufferkf = yes|no (whether the plugin should store the latest
keyframe and send it immediately for new viewers, EXPERIMENTAL)
dataport = local port for receiving data messages to relay
dataiface = network interface or IP address to bind to, if any (binds to all otherwise)
databuffermsg = yes|no (whether the plugin should store the latest
message and send it immediately for new viewers)
The following options are only valid for the 'rstp' type:
url = RTSP stream URL (only if type=rtsp)
rtsp_user = RTSP authorization username (only if type=rtsp)
rtsp_pwd = RTSP authorization password (only if type=rtsp)
rtspiface = network interface IP address or device name to listen on when receiving RTSP streams
\endverbatim
*
* \section streamapi Streaming API
*
* The Streaming 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 list , \c create , \c destroy , \c recording , \c enable and
* \c disable are synchronous requests, which means you'll
* get a response directly within the context of the transaction. \c list
* lists all the available streams; \c create allows you to create a new
* mountpoint dynamically, as an alternative to using the configuration
* file; \c destroy removes a mountpoint and destroys it; \c recording
* instructs the plugin on whether or not a live RTP stream should be
* recorded while it's broadcasted; \c enable and \c disable respectively
* enable and disable a mountpoint, that is decide whether or not a
* mountpoint should be available to users without destroying it.
*
* The \c watch , \c start , \c pause , \c switch and \c stop requests
* instead are all asynchronous, which means you'll get a notification
* about their success or failure in an event. \c watch asks the plugin
* to prepare the playout of one of the available streams; \c start
* starts the actual playout; \c pause allows you to pause a playout
* without tearing down the PeerConnection; \c switch allows you to
* switch to a different mountpoint of the same kind (note: only live
* RTP mountpoints supported as of now) without having to stop and watch
* the new one; \c stop stops the playout and tears the PeerConnection
* down.
*
* Notice that, in general, all users can create mountpoints, no matter
* what type they are. 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 <errno.h>
#include <sys/poll.h>
#include <sys/time.h>
#ifdef HAVE_LIBCURL
#include <curl/curl.h>
#endif
#include "../debug.h"
#include "../apierror.h"
#include "../config.h"
#include "../mutex.h"
#include "../rtp.h"
#include "../rtcp.h"
#include "../record.h"
#include "../utils.h"
#include "../ip-utils.h"
/* Plugin information */
#define JANUS_STREAMING_VERSION 7
#define JANUS_STREAMING_VERSION_STRING "0.0.7"
#define JANUS_STREAMING_DESCRIPTION "This is a streaming plugin for Janus, allowing WebRTC peers to watch/listen to pre-recorded files or media generated by gstreamer."
#define JANUS_STREAMING_NAME "JANUS Streaming plugin"
#define JANUS_STREAMING_AUTHOR "Meetecho s.r.l."
#define JANUS_STREAMING_PACKAGE "janus.plugin.streaming"
/* Plugin methods */
janus_plugin *create(void);
int janus_streaming_init(janus_callbacks *callback, const char *config_path);
void janus_streaming_destroy(void);
int janus_streaming_get_api_compatibility(void);
int janus_streaming_get_version(void);
const char *janus_streaming_get_version_string(void);
const char *janus_streaming_get_description(void);
const char *janus_streaming_get_name(void);
const char *janus_streaming_get_author(void);
const char *janus_streaming_get_package(void);
void janus_streaming_create_session(janus_plugin_session *handle, int *error);
struct janus_plugin_result *janus_streaming_handle_message(janus_plugin_session *handle, char *transaction, json_t *message, json_t *jsep);
void janus_streaming_setup_media(janus_plugin_session *handle);
void janus_streaming_incoming_rtp(janus_plugin_session *handle, int video, char *buf, int len);
void janus_streaming_incoming_rtcp(janus_plugin_session *handle, int video, char *buf, int len);
void janus_streaming_hangup_media(janus_plugin_session *handle);
void janus_streaming_destroy_session(janus_plugin_session *handle, int *error);
json_t *janus_streaming_query_session(janus_plugin_session *handle);
static int janus_streaming_get_fd_port(int fd);
/* Plugin setup */
static janus_plugin janus_streaming_plugin =
JANUS_PLUGIN_INIT (
.init = janus_streaming_init,
.destroy = janus_streaming_destroy,
.get_api_compatibility = janus_streaming_get_api_compatibility,
.get_version = janus_streaming_get_version,
.get_version_string = janus_streaming_get_version_string,
.get_description = janus_streaming_get_description,
.get_name = janus_streaming_get_name,
.get_author = janus_streaming_get_author,
.get_package = janus_streaming_get_package,
.create_session = janus_streaming_create_session,
.handle_message = janus_streaming_handle_message,
.setup_media = janus_streaming_setup_media,
.incoming_rtp = janus_streaming_incoming_rtp,
.incoming_rtcp = janus_streaming_incoming_rtcp,
.hangup_media = janus_streaming_hangup_media,
.destroy_session = janus_streaming_destroy_session,
.query_session = janus_streaming_query_session,
);
/* Plugin creator */
janus_plugin *create(void) {
JANUS_LOG(LOG_VERB, "%s created!\n", JANUS_STREAMING_NAME);
return &janus_streaming_plugin;
}
/* Parameter validation */
static struct janus_json_parameter request_parameters[] = {
{"request", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter id_parameters[] = {
{"id", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE}
};
static struct janus_json_parameter adminkey_parameters[] = {
{"admin_key", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter create_parameters[] = {
{"type", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"secret", JSON_STRING, 0},
{"pin", JSON_STRING, 0},
{"permanent", JANUS_JSON_BOOL, 0}
};
static struct janus_json_parameter rtp_parameters[] = {
{"id", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"name", JSON_STRING, 0},
{"description", JSON_STRING, 0},
{"is_private", JANUS_JSON_BOOL, 0},
{"audio", JANUS_JSON_BOOL, 0},
{"video", JANUS_JSON_BOOL, 0}
};
static struct janus_json_parameter live_parameters[] = {
{"id", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"name", JSON_STRING, 0},
{"description", JSON_STRING, 0},
{"is_private", JANUS_JSON_BOOL, 0},
{"filename", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"audio", JANUS_JSON_BOOL, 0},
{"video", JANUS_JSON_BOOL, 0}
};
static struct janus_json_parameter ondemand_parameters[] = {
{"id", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"name", JSON_STRING, 0},
{"description", JSON_STRING, 0},
{"is_private", JANUS_JSON_BOOL, 0},
{"filename", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"audio", JANUS_JSON_BOOL, 0},
{"video", JANUS_JSON_BOOL, 0}
};
#ifdef HAVE_LIBCURL
static struct janus_json_parameter rtsp_parameters[] = {
{"id", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
{"name", JSON_STRING, 0},
{"description", JSON_STRING, 0},
{"is_private", JANUS_JSON_BOOL, 0},
{"url", JSON_STRING, 0},
{"rtsp_user", JSON_STRING, 0},
{"rtsp_pwd", JSON_STRING, 0},
{"audio", JANUS_JSON_BOOL, 0},
{"video", JANUS_JSON_BOOL, 0},
{"rtspiface", JSON_STRING, 0}
};
#endif
static struct janus_json_parameter rtp_audio_parameters[] = {
{"audiomcast", JSON_STRING, 0},
{"audioport", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"audiopt", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"audiortpmap", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"audiofmtp", JSON_STRING, 0},
{"audioiface", JSON_STRING, 0}
};
static struct janus_json_parameter rtp_video_parameters[] = {
{"videomcast", JSON_STRING, 0},
{"videoport", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"videopt", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"videortpmap", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"videofmtp", JSON_STRING, 0},
{"videobufferkf", JANUS_JSON_BOOL, 0},
{"videoiface", JSON_STRING, 0}
};
static struct janus_json_parameter rtp_data_parameters[] = {
{"dataport", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"databuffermsg", JANUS_JSON_BOOL, 0},
{"dataiface", JSON_STRING, 0}
};
static struct janus_json_parameter destroy_parameters[] = {
{"id", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"permanent", JANUS_JSON_BOOL, 0}
};
static struct janus_json_parameter recording_parameters[] = {
{"id", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"action", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter recording_start_parameters[] = {
{"audio", JSON_STRING, 0},
{"video", JSON_STRING, 0},
{"data", JSON_STRING, 0}
};
static struct janus_json_parameter recording_stop_parameters[] = {
{"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_streaming_handler(void *data);
static void *janus_streaming_ondemand_thread(void *data);
static void *janus_streaming_filesource_thread(void *data);
static void janus_streaming_relay_rtp_packet(gpointer data, gpointer user_data);
static void *janus_streaming_relay_thread(void *data);
static gboolean janus_streaming_is_keyframe(gint codec, char* buffer, int len);
typedef enum janus_streaming_type {
janus_streaming_type_none = 0,
janus_streaming_type_live,
janus_streaming_type_on_demand,
} janus_streaming_type;
typedef enum janus_streaming_source {
janus_streaming_source_none = 0,
janus_streaming_source_file,
janus_streaming_source_rtp,
} janus_streaming_source;
typedef struct janus_streaming_rtp_keyframe {
gboolean enabled;
/* If enabled, we store the packets of the last keyframe, to immediately send them for new viewers */
GList *latest_keyframe;
/* This is where we store packets while we're still collecting the whole keyframe */
GList *temp_keyframe;
guint32 temp_ts;
janus_mutex mutex;
} janus_streaming_rtp_keyframe;
#ifdef HAVE_LIBCURL
typedef struct janus_streaming_buffer {
char *buffer;
size_t size;
} janus_streaming_buffer;
#endif
typedef struct janus_streaming_rtp_source {
gint audio_port;
in_addr_t audio_mcast;
gint video_port;
in_addr_t video_mcast;
gint data_port;
janus_recorder *arc; /* The Janus recorder instance for this streams's audio, if enabled */
janus_recorder *vrc; /* The Janus recorder instance for this streams's video, if enabled */
janus_recorder *drc; /* The Janus recorder instance for this streams's data, if enabled */
janus_mutex rec_mutex; /* Mutex to protect the recorders from race conditions */
int audio_fd;
int video_fd;
int data_fd;
gint64 last_received_audio;
gint64 last_received_video;
gint64 last_received_data;
#ifdef HAVE_LIBCURL
gboolean rtsp;
CURL *curl;
janus_streaming_buffer *curldata;
char *rtsp_url;
char *rtsp_username, *rtsp_password;
int ka_timeout;
gboolean reconnecting;
gint64 reconnect_timer;
janus_mutex rtsp_mutex;
int audio_rtcp_fd;
int video_rtcp_fd;
#endif
janus_streaming_rtp_keyframe keyframe;
gboolean buffermsg;
void *last_msg;
janus_mutex buffermsg_mutex;
janus_network_address audio_iface;
janus_network_address video_iface;
janus_network_address data_iface;
} janus_streaming_rtp_source;
typedef struct janus_streaming_file_source {
char *filename;
} janus_streaming_file_source;
/* used for audio/video fd and rtcp fd */
typedef struct multiple_fds {
int fd;
int rtcp_fd;
} multiple_fds;
#define JANUS_STREAMING_VP8 0
#define JANUS_STREAMING_H264 1
#define JANUS_STREAMING_VP9 2
typedef struct janus_streaming_codecs {
gint audio_pt;
char *audio_rtpmap;
char *audio_fmtp;
gint video_codec;
gint video_pt;
char *video_rtpmap;
char *video_fmtp;
} janus_streaming_codecs;
typedef struct janus_streaming_mountpoint {
guint64 id;
char *name;
char *description;
gboolean is_private;
char *secret;
char *pin;
gboolean enabled;
gboolean active;
janus_streaming_type streaming_type;
janus_streaming_source streaming_source;
void *source; /* Can differ according to the source type */
GDestroyNotify source_destroy;
janus_streaming_codecs codecs;
gboolean audio, video, data;
GList/*<unowned janus_streaming_session>*/ *listeners;
gint64 destroyed;
janus_mutex mutex;
} janus_streaming_mountpoint;
GHashTable *mountpoints;
static GList *old_mountpoints;
janus_mutex mountpoints_mutex;
static char *admin_key = NULL;
static void janus_streaming_mountpoint_free(janus_streaming_mountpoint *mp);
/* Helper to create an RTP live source (e.g., from gstreamer/ffmpeg/vlc/etc.) */
janus_streaming_mountpoint *janus_streaming_create_rtp_source(
uint64_t id, char *name, char *desc,
gboolean doaudio, char* amcast, const janus_network_address *aiface, uint16_t aport, uint8_t acodec, char *artpmap, char *afmtp,
gboolean dovideo, char* vmcast, const janus_network_address *viface, uint16_t vport, uint8_t vcodec, char *vrtpmap, char *vfmtp, gboolean bufferkf,
gboolean dodata, const janus_network_address *diface, uint16_t dport, gboolean buffermsg);
/* Helper to create a file/ondemand live source */
janus_streaming_mountpoint *janus_streaming_create_file_source(
uint64_t id, char *name, char *desc, char *filename,
gboolean live, gboolean doaudio, gboolean dovideo);
/* Helper to create a rtsp live source */
janus_streaming_mountpoint *janus_streaming_create_rtsp_source(
uint64_t id, char *name, char *desc,
char *url, char *username, char *password,
gboolean doaudio, gboolean dovideo, const janus_network_address *iface);
typedef struct janus_streaming_message {
janus_plugin_session *handle;
char *transaction;
json_t *message;
json_t *jsep;
} janus_streaming_message;
static GAsyncQueue *messages = NULL;
static janus_streaming_message exit_message;
static void janus_streaming_message_free(janus_streaming_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);
}
typedef struct janus_streaming_session {
janus_plugin_session *handle;
janus_streaming_mountpoint *mountpoint;
gboolean started;
gboolean paused;
janus_rtp_switching_context context;
gboolean stopping;
volatile gint hangingup;
gint64 destroyed; /* Time at which this session was marked as destroyed */
} janus_streaming_session;
static GHashTable *sessions;
static GList *old_sessions;
static janus_mutex sessions_mutex;
/* Packets we get from outside and relay */
typedef struct janus_streaming_rtp_relay_packet {
rtp_header *data;
gint length;
gboolean is_rtp; /* This may be a data packet and not RTP */
gboolean is_video;
gboolean is_keyframe;
uint32_t timestamp;
uint16_t seq_number;
} janus_streaming_rtp_relay_packet;
/* Error codes */
#define JANUS_STREAMING_ERROR_NO_MESSAGE 450
#define JANUS_STREAMING_ERROR_INVALID_JSON 451
#define JANUS_STREAMING_ERROR_INVALID_REQUEST 452
#define JANUS_STREAMING_ERROR_MISSING_ELEMENT 453
#define JANUS_STREAMING_ERROR_INVALID_ELEMENT 454
#define JANUS_STREAMING_ERROR_NO_SUCH_MOUNTPOINT 455
#define JANUS_STREAMING_ERROR_CANT_CREATE 456
#define JANUS_STREAMING_ERROR_UNAUTHORIZED 457
#define JANUS_STREAMING_ERROR_CANT_SWITCH 458
#define JANUS_STREAMING_ERROR_CANT_RECORD 459
#define JANUS_STREAMING_ERROR_UNKNOWN_ERROR 470
/* Streaming watchdog/garbage collector (sort of) */
static void *janus_streaming_watchdog(void *data) {
JANUS_LOG(LOG_INFO, "Streaming watchdog started\n");
gint64 now = 0;
while(g_atomic_int_get(&initialized) && !g_atomic_int_get(&stopping)) {
janus_mutex_lock(&sessions_mutex);
/* Iterate on all the sessions */
now = janus_get_monotonic_time();
if(old_sessions != NULL) {
GList *sl = old_sessions;
JANUS_LOG(LOG_HUGE, "Checking %d old Streaming sessions...\n", g_list_length(old_sessions));
while(sl) {
janus_streaming_session *session = (janus_streaming_session *)sl->data;
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 Streaming session\n");
GList *rm = sl->next;
old_sessions = g_list_delete_link(old_sessions, sl);
sl = rm;
session->handle = NULL;
g_free(session);
session = NULL;
continue;
}
sl = sl->next;
}
}
janus_mutex_unlock(&sessions_mutex);
janus_mutex_lock(&mountpoints_mutex);
/* Iterate on all the mountpoints */
if(old_mountpoints != NULL) {
GList *sl = old_mountpoints;
JANUS_LOG(LOG_HUGE, "Checking %d old Streaming mountpoints...\n", g_list_length(old_mountpoints));
while(sl) {
janus_streaming_mountpoint *mountpoint = (janus_streaming_mountpoint *)sl->data;
if(!mountpoint) {
sl = sl->next;
continue;
}
if(now-mountpoint->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 Streaming mountpoint\n");
GList *rm = sl->next;
old_mountpoints = g_list_delete_link(old_mountpoints, sl);
sl = rm;
janus_streaming_mountpoint_free(mountpoint);
mountpoint = NULL;
continue;
}
sl = sl->next;
}
}
janus_mutex_unlock(&mountpoints_mutex);
g_usleep(500000);
}
JANUS_LOG(LOG_INFO, "Streaming watchdog stopped\n");
return NULL;
}
/* Plugin implementation */
int janus_streaming_init(janus_callbacks *callback, const char *config_path) {
#ifdef HAVE_LIBCURL
curl_global_init(CURL_GLOBAL_ALL);
#endif
if(g_atomic_int_get(&stopping)) {
/* Still stopping from before */
return -1;
}
if(callback == NULL || config_path == NULL) {
/* Invalid arguments */
return -1;
}
struct ifaddrs *ifas = NULL;
if(getifaddrs(&ifas) || ifas == NULL) {
JANUS_LOG(LOG_ERR, "Unable to acquire list of network devices/interfaces; some configurations may not work as expected...\n");
}
/* Read configuration */
char filename[255];
g_snprintf(filename, 255, "%s/%s.cfg", config_path, JANUS_STREAMING_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);
mountpoints = g_hash_table_new_full(g_int64_hash, g_int64_equal, (GDestroyNotify)g_free, NULL);
janus_mutex_init(&mountpoints_mutex);
/* Parse configuration to populate the mountpoints */
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_STREAMING_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 stream '%s'\n", cat->name);
janus_config_item *type = janus_config_get_item(cat, "type");
if(type == NULL || type->value == NULL) {
JANUS_LOG(LOG_WARN, " -- Invalid type, skipping stream '%s'...\n", cat->name);
cl = cl->next;
continue;
}
if(!strcasecmp(type->value, "rtp")) {
janus_network_address video_iface, audio_iface, data_iface;
/* RTP live source (e.g., from gstreamer/ffmpeg/vlc/etc.) */
janus_config_item *id = janus_config_get_item(cat, "id");
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 *audio = janus_config_get_item(cat, "audio");
janus_config_item *video = janus_config_get_item(cat, "video");
janus_config_item *data = janus_config_get_item(cat, "data");
janus_config_item *diface = janus_config_get_item(cat, "dataiface");
janus_config_item *amcast = janus_config_get_item(cat, "audiomcast");
janus_config_item *aiface = janus_config_get_item(cat, "audioiface");
janus_config_item *aport = janus_config_get_item(cat, "audioport");
janus_config_item *acodec = janus_config_get_item(cat, "audiopt");
janus_config_item *artpmap = janus_config_get_item(cat, "audiortpmap");
janus_config_item *afmtp = janus_config_get_item(cat, "audiofmtp");
janus_config_item *vmcast = janus_config_get_item(cat, "videomcast");
janus_config_item *viface = janus_config_get_item(cat, "videoiface");
janus_config_item *vport = janus_config_get_item(cat, "videoport");
janus_config_item *vcodec = janus_config_get_item(cat, "videopt");
janus_config_item *vrtpmap = janus_config_get_item(cat, "videortpmap");
janus_config_item *vfmtp = janus_config_get_item(cat, "videofmtp");
janus_config_item *vkf = janus_config_get_item(cat, "videobufferkf");
janus_config_item *dport = janus_config_get_item(cat, "dataport");
janus_config_item *dbm = janus_config_get_item(cat, "databuffermsg");
gboolean is_private = priv && priv->value && janus_is_true(priv->value);
gboolean doaudio = audio && audio->value && janus_is_true(audio->value);
gboolean dovideo = video && video->value && janus_is_true(video->value);
gboolean dodata = data && data->value && janus_is_true(data->value);
gboolean bufferkf = video && vkf && vkf->value && janus_is_true(vkf->value);
gboolean buffermsg = data && dbm && dbm->value && janus_is_true(dbm->value);
if(!doaudio && !dovideo && !dodata) {
JANUS_LOG(LOG_ERR, "Can't add 'rtp' stream '%s', no audio, video or data have to be streamed...\n", cat->name);
cl = cl->next;
continue;
}
if(doaudio &&
(aport == NULL || aport->value == NULL || atoi(aport->value) == 0 ||
acodec == NULL || acodec->value == NULL ||
artpmap == NULL || artpmap->value == NULL)) {
JANUS_LOG(LOG_ERR, "Can't add 'rtp' stream '%s', missing mandatory information for audio...\n", cat->name);
cl = cl->next;
continue;
}
if(doaudio && aiface) {
if(!ifas) {
JANUS_LOG(LOG_ERR, "Skipping 'rtp' stream '%s', it relies on network configuration but network device information is unavailable...\n", cat->name);
cl = cl->next;
continue;
}
if(janus_network_lookup_interface(ifas, aiface->value, &audio_iface) != 0) {
JANUS_LOG(LOG_ERR, "Can't add 'rtp' stream '%s', invalid network interface configuration for audio...\n", cat->name);
cl = cl->next;
continue;
}
}
if(dovideo &&
(vport == NULL || vport->value == NULL || atoi(vport->value) == 0 ||
vcodec == NULL || vcodec->value == NULL ||
vrtpmap == NULL || vrtpmap->value == NULL)) {
JANUS_LOG(LOG_ERR, "Can't add 'rtp' stream '%s', missing mandatory information for video...\n", cat->name);
cl = cl->next;
continue;
}
if(dodata && (dport == NULL || dport->value == NULL || atoi(dport->value) == 0)) {
JANUS_LOG(LOG_ERR, "Can't add 'rtp' stream '%s', missing mandatory information for data...\n", cat->name);
cl = cl->next;
continue;
}
#ifndef HAVE_SCTP
if(dodata) {
JANUS_LOG(LOG_ERR, "Can't add 'rtp' stream '%s': no datachannels support......\n", cat->name);
cl = cl->next;
continue;
}
#endif
if(dodata && diface) {
if(!ifas) {
JANUS_LOG(LOG_ERR, "Skipping 'rtp' stream '%s', it relies on network configuration but network device information is unavailable...\n", cat->name);
cl = cl->next;
continue;
}
if(janus_network_lookup_interface(ifas, diface->value, &data_iface) != 0) {
JANUS_LOG(LOG_ERR, "Can't add 'rtp' stream '%s', invalid network interface configuration for data...\n", cat->name);
cl = cl->next;
continue;
}
}
if(dovideo && viface) {
if(!ifas) {
JANUS_LOG(LOG_ERR, "Skipping 'rtp' stream '%s', it relies on network configuration but network device information is unavailable...\n", cat->name);
cl = cl->next;
continue;
}
if(janus_network_lookup_interface(ifas, viface->value, &video_iface) != 0) {
JANUS_LOG(LOG_ERR, "Can't add 'rtp' stream '%s', invalid network interface configuration for video...\n", cat->name);
cl = cl->next;
continue;
}
}
if(id == NULL || id->value == NULL) {
JANUS_LOG(LOG_VERB, "Missing id for stream '%s', will generate a random one...\n", cat->name);
} else {
janus_mutex_lock(&mountpoints_mutex);
guint64 mpid = g_ascii_strtoull(id->value, 0, 10);
janus_streaming_mountpoint *mp = g_hash_table_lookup(mountpoints, &mpid);
janus_mutex_unlock(&mountpoints_mutex);
if(mp != NULL) {
JANUS_LOG(LOG_ERR, "A stream with the provided ID %s already exists, skipping '%s'\n", id->value, cat->name);
cl = cl->next;
continue;
}
}
JANUS_LOG(LOG_VERB, "Audio %s, Video %s, Data %s\n",
doaudio ? "enabled" : "NOT enabled",
dovideo ? "enabled" : "NOT enabled",
dodata ? "enabled" : "NOT enabled");
janus_streaming_mountpoint *mp = NULL;
if((mp = janus_streaming_create_rtp_source(
(id && id->value) ? g_ascii_strtoull(id->value, 0, 10) : 0,
(char *)cat->name,
desc ? (char *)desc->value : NULL,
doaudio,
amcast ? (char *)amcast->value : NULL,
doaudio && aiface && aiface->value ? &audio_iface : NULL,
(aport && aport->value) ? atoi(aport->value) : 0,
(acodec && acodec->value) ? atoi(acodec->value) : 0,
artpmap ? (char *)artpmap->value : NULL,
afmtp ? (char *)afmtp->value : NULL,
dovideo,
vmcast ? (char *)vmcast->value : NULL,
dovideo && viface && viface->value ? &video_iface : NULL,
(vport && vport->value) ? atoi(vport->value) : 0,
(vcodec && vcodec->value) ? atoi(vcodec->value) : 0,
vrtpmap ? (char *)vrtpmap->value : NULL,
vfmtp ? (char *)vfmtp->value : NULL,
bufferkf,
dodata,
dodata && diface && diface->value ? &data_iface : NULL,
(dport && dport->value) ? atoi(dport->value) : 0,
buffermsg)) == NULL) {
JANUS_LOG(LOG_ERR, "Error creating 'rtp' stream '%s'...\n", cat->name);
cl = cl->next;
continue;
}
mp->is_private = is_private;
if(secret && secret->value)
mp->secret = g_strdup(secret->value);
if(pin && pin->value)
mp->pin = g_strdup(pin->value);
} else if(!strcasecmp(type->value, "live")) {
/* File live source */
janus_config_item *id = janus_config_get_item(cat, "id");
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 *file = janus_config_get_item(cat, "filename");
janus_config_item *audio = janus_config_get_item(cat, "audio");
janus_config_item *video = janus_config_get_item(cat, "video");
if(file == NULL || file->value == NULL) {
JANUS_LOG(LOG_ERR, "Can't add 'live' stream '%s', missing mandatory information...\n", cat->name);
cl = cl->next;
continue;
}
gboolean is_private = priv && priv->value && janus_is_true(priv->value);
gboolean doaudio = audio && audio->value && janus_is_true(audio->value);
gboolean dovideo = video && video->value && janus_is_true(video->value);
/* TODO We should support something more than raw a-Law and mu-Law streams... */
if(!doaudio || dovideo) {
JANUS_LOG(LOG_ERR, "Can't add 'live' stream '%s', we only support audio file streaming right now...\n", cat->name);
cl = cl->next;
continue;
}
if(!strstr(file->value, ".alaw") && !strstr(file->value, ".mulaw")) {
JANUS_LOG(LOG_ERR, "Can't add 'live' stream '%s', unsupported format (we only support raw mu-Law and a-Law files right now)\n", cat->name);
cl = cl->next;
continue;
}
FILE *audiofile = fopen(file->value, "rb");
if(!audiofile) {
JANUS_LOG(LOG_ERR, "Can't add 'live' stream, no such file '%s'...\n", file->value);
cl = cl->next;
continue;
}
fclose(audiofile);
if(id == NULL || id->value == NULL) {
JANUS_LOG(LOG_VERB, "Missing id for stream '%s', will generate a random one...\n", cat->name);
} else {
janus_mutex_lock(&mountpoints_mutex);
guint64 mpid = g_ascii_strtoull(id->value, 0, 10);
janus_streaming_mountpoint *mp = g_hash_table_lookup(mountpoints, &mpid);
janus_mutex_unlock(&mountpoints_mutex);
if(mp != NULL) {
JANUS_LOG(LOG_ERR, "A stream with the provided ID %s already exists, skipping '%s'\n", id->value, cat->name);
cl = cl->next;
continue;
}
}
janus_streaming_mountpoint *mp = NULL;
if((mp = janus_streaming_create_file_source(
(id && id->value) ? g_ascii_strtoull(id->value, 0, 10) : 0,
(char *)cat->name,
desc ? (char *)desc->value : NULL,
(char *)file->value,
TRUE, doaudio, dovideo)) == NULL) {
JANUS_LOG(LOG_ERR, "Error creating 'live' stream '%s'...\n", cat->name);
cl = cl->next;
continue;
}
mp->is_private = is_private;
if(secret && secret->value)
mp->secret = g_strdup(secret->value);
if(pin && pin->value)
mp->pin = g_strdup(pin->value);
} else if(!strcasecmp(type->value, "ondemand")) {
/* mu-Law file on demand source */
janus_config_item *id = janus_config_get_item(cat, "id");
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 *file = janus_config_get_item(cat, "filename");
janus_config_item *audio = janus_config_get_item(cat, "audio");
janus_config_item *video = janus_config_get_item(cat, "video");
if(file == NULL || file->value == NULL) {
JANUS_LOG(LOG_ERR, "Can't add 'ondemand' stream '%s', missing mandatory information...\n", cat->name);
cl = cl->next;
continue;
}
gboolean is_private = priv && priv->value && janus_is_true(priv->value);
gboolean doaudio = audio && audio->value && janus_is_true(audio->value);
gboolean dovideo = video && video->value && janus_is_true(video->value);
/* TODO We should support something more than raw a-Law and mu-Law streams... */
if(!doaudio || dovideo) {
JANUS_LOG(LOG_ERR, "Can't add 'ondemand' stream '%s', we only support audio file streaming right now...\n", cat->name);
cl = cl->next;
continue;
}
if(!strstr(file->value, ".alaw") && !strstr(file->value, ".mulaw")) {
JANUS_LOG(LOG_ERR, "Can't add 'ondemand' stream '%s', unsupported format (we only support raw mu-Law and a-Law files right now)\n", cat->name);
cl = cl->next;
continue;
}
FILE *audiofile = fopen(file->value, "rb");
if(!audiofile) {
JANUS_LOG(LOG_ERR, "Can't add 'ondemand' stream, no such file '%s'...\n", file->value);
cl = cl->next;
continue;
}
fclose(audiofile);
if(id == NULL || id->value == NULL) {
JANUS_LOG(LOG_VERB, "Missing id for stream '%s', will generate a random one...\n", cat->name);
} else {
janus_mutex_lock(&mountpoints_mutex);
guint64 mpid = g_ascii_strtoull(id->value, 0, 10);
janus_streaming_mountpoint *mp = g_hash_table_lookup(mountpoints, &mpid);
janus_mutex_unlock(&mountpoints_mutex);
if(mp != NULL) {
JANUS_LOG(LOG_ERR, "A stream with the provided ID %s already exists, skipping '%s'\n", id->value, cat->name);
cl = cl->next;
continue;
}
}
janus_streaming_mountpoint *mp = NULL;
if((mp = janus_streaming_create_file_source(
(id && id->value) ? g_ascii_strtoull(id->value, 0, 10) : 0,
(char *)cat->name,
desc ? (char *)desc->value : NULL,
(char *)file->value,
FALSE, doaudio, dovideo)) == NULL) {
JANUS_LOG(LOG_ERR, "Error creating 'ondemand' stream '%s'...\n", cat->name);
cl = cl->next;
continue;
}
mp->is_private = is_private;
if(secret && secret->value)
mp->secret = g_strdup(secret->value);
if(pin && pin->value)
mp->pin = g_strdup(pin->value);
} else if(!strcasecmp(type->value, "rtsp")) {
#ifndef HAVE_LIBCURL
JANUS_LOG(LOG_ERR, "Can't add 'rtsp' stream '%s', libcurl support not compiled...\n", cat->name);
cl = cl->next;
continue;
#else
janus_config_item *id = janus_config_get_item(cat, "id");
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 *file = janus_config_get_item(cat, "url");
janus_config_item *username = janus_config_get_item(cat, "rtsp_user");
janus_config_item *password = janus_config_get_item(cat, "rtsp_pwd");
janus_config_item *audio = janus_config_get_item(cat, "audio");
janus_config_item *video = janus_config_get_item(cat, "video");
janus_config_item *iface = janus_config_get_item(cat, "rtspiface");
janus_network_address iface_value;
if(file == NULL || file->value == NULL) {
JANUS_LOG(LOG_ERR, "Can't add 'rtsp' stream '%s', missing mandatory information...\n", cat->name);
cl = cl->next;
continue;
}
gboolean is_private = priv && priv->value && janus_is_true(priv->value);
gboolean doaudio = audio && audio->value && janus_is_true(audio->value);
gboolean dovideo = video && video->value && janus_is_true(video->value);
if((doaudio || dovideo) && iface && iface->value) {
if(!ifas) {
JANUS_LOG(LOG_ERR, "Skipping 'rtsp' stream '%s', it relies on network configuration but network device information is unavailable...\n", cat->name);
cl = cl->next;
continue;
}
if(janus_network_lookup_interface(ifas, iface->value, &iface_value) != 0) {
JANUS_LOG(LOG_ERR, "Can't add 'rtsp' stream '%s', invalid network interface configuration for stream...\n", cat->name);
cl = cl->next;
continue;
}
}
if(id == NULL || id->value == NULL) {
JANUS_LOG(LOG_VERB, "Missing id for stream '%s', will generate a random one...\n", cat->name);
} else {
janus_mutex_lock(&mountpoints_mutex);
guint64 mpid = g_ascii_strtoull(id->value, 0, 10);
janus_streaming_mountpoint *mp = g_hash_table_lookup(mountpoints, &mpid);
janus_mutex_unlock(&mountpoints_mutex);
if(mp != NULL) {
JANUS_LOG(LOG_ERR, "A stream with the provided ID %s already exists, skipping '%s'\n", id->value, cat->name);
cl = cl->next;
continue;
}
}
janus_streaming_mountpoint *mp = NULL;
if((mp = janus_streaming_create_rtsp_source(
(id && id->value) ? g_ascii_strtoull(id->value, 0, 10) : 0,
(char *)cat->name,
desc ? (char *)desc->value : NULL,
(char *)file->value,
username ? (char *)username->value : NULL,
password ? (char *)password->value : NULL,
doaudio, dovideo,
iface && iface->value ? &iface_value : NULL)) == NULL) {
JANUS_LOG(LOG_ERR, "Error creating 'rtsp' stream '%s'...\n", cat->name);