Statistics
| Branch: | Revision:

janus-gateway / plugins / janus_videoroom.c @ 71a04f89

History | View | Annotate | Download (173 KB)

1
/*! \file   janus_videoroom.c
2
 * \author Lorenzo Miniero <lorenzo@meetecho.com>
3
 * \copyright GNU General Public License v3
4
 * \brief  Janus VideoRoom plugin
5
 * \details  This is a plugin implementing a videoconferencing SFU
6
 * (Selective Forwarding Unit) for Janus, that is an audio/video router.
7
 * This means that the plugin implements a virtual conferencing room peers
8
 * can join and leave at any time. This room is based on a Publish/Subscribe
9
 * pattern. Each peer can publish his/her own live audio/video feeds: this
10
 * feed becomes an available stream in the room the other participants can
11
 * attach to. This means that this plugin allows the realization of several
12
 * different scenarios, ranging from a simple webinar (one speaker, several
13
 * listeners) to a fully meshed video conference (each peer sending and
14
 * receiving to and from all the others).
15
 * 
16
 * For what concerns the subscriber side, there are two different ways to
17
 * attach to a publisher's feed: a generic 'listener', which can attach to
18
 * a single feed, and a more complex 'Multiplexed listener', which instead can
19
 * attach to more feeds using the same PeerConnection. The generic 'listener'
20
 * is the default, which means that if you want to watch more feeds at the
21
 * same time, you'll need to create multiple 'listeners' to attach at any
22
 * of them. The 'Multiplexed listener', instead, is a more complex alternative
23
 * that exploits the so called RTCWEB 'Plan B', which multiplexes more
24
 * streams on a single PeerConnection and in the SDP: while more efficient in terms of
25
 * resources, though, this approach is experimental, and currently only
26
 * available on Google Chrome, so use it wisely.
27
 * \note As of now, work on Plan B is still going on, and as such its support in Janus
28
 * is flaky to say the least. Don't try to attach as a Multiplexed listener or bad
29
 * things will probably happen!
30
 * 
31
 * Considering that this plugin allows for several different WebRTC PeerConnections
32
 * to be on at the same time for the same peer (specifically, each peer
33
 * potentially has 1 PeerConnection on for publishing and N on for subscriptions
34
 * from other peers), each peer may need to attach several times to the same
35
 * plugin for every stream: this means that each peer needs to have at least one
36
 * handle active for managing its relation with the plugin (joining a room,
37
 * leaving a room, muting/unmuting, publishing, receiving events), and needs
38
 * to open a new one each time he/she wants to subscribe to a feed from
39
 * another participant (or a single one in case a 'Multiplexed listener is used).
40
 * The handle used for a subscription, however, would be logically a "slave"
41
 * to the master one used for managing the room: this means that it cannot
42
 * be used, for instance, to unmute in the room, as its only purpose would
43
 * be to provide a context in which creating the sendonly PeerConnection
44
 * for the subscription to the active participant.
45
 * 
46
 * Rooms to make available are listed in the plugin configuration file.
47
 * A pre-filled configuration file is provided in \c conf/janus.plugin.videoroom.cfg
48
 * and includes a demo room for testing. The same plugin is also used
49
 * dynamically (that is, with rooms created on the fly via API) in the
50
 * Screen Sharing demo as well.
51
 * 
52
 * To add more rooms or modify the existing one, you can use the following
53
 * syntax:
54
 * 
55
 * \verbatim
56
[<unique room ID>]
57
description = This is my awesome room
58
is_private = yes|no (private rooms don't appear when you do a 'list' request)
59
secret = <optional password needed for manipulating (e.g. destroying) the room>
60
pin = <optional password needed for joining the room>
61
publishers = <max number of concurrent senders> (e.g., 6 for a video
62
             conference or 1 for a webinar)
63
bitrate = <max video bitrate for senders> (e.g., 128000)
64
fir_freq = <send a FIR to publishers every fir_freq seconds> (0=disable)
65
audiocodec = opus|isac32|isac16|pcmu|pcma (audio codec to force on publishers, default=opus)
66
videocodec = vp8|vp9|h264 (video codec to force on publishers, default=vp8)
67
record = true|false (whether this room should be recorded, default=false)
68
rec_dir = <folder where recordings should be stored, when enabled>
69
\endverbatim
70
 *
71
 * Note that recording will work with all codecs except iSAC.
72
 *
73
 * \section sfuapi Video Room API
74
 * 
75
 * The Video Room API supports several requests, some of which are
76
 * synchronous and some asynchronous. There are some situations, though,
77
 * (invalid JSON, invalid request) which will always result in a
78
 * synchronous error response even for asynchronous requests. 
79
 * 
80
 * \c create , \c destroy , \c exists, \c list and \c listparticipants
81
 * are synchronous requests, which means you'll
82
 * get a response directly within the context of the transaction.
83
 * \c create allows you to create a new video room dynamically, as an
84
 * alternative to using the configuration file; \c destroy removes a
85
 * video room and destroys it, kicking all the users out as part of the
86
 * process; \c exists allows you to check whether a specific video room
87
 * exists; finally, \c list lists all the available rooms, while \c
88
 * listparticipants lists all the participants of a specific room and
89
 * their details.
90
 * 
91
 * The \c join , \c joinandconfigure , \c configure , \c publish ,
92
 * \c unpublish , \c start , \c pause , \c switch , \c stop , \c add ,
93
 * \c remove and \c leave requests instead are all asynchronous, which
94
 * means you'll get a notification about their success or failure in
95
 * an event. \c join allows you to join a specific video room, specifying
96
 * whether that specific PeerConnection will be used for publishing or
97
 * watching; \c configure can be used to modify some of the participation
98
 * settings (e.g., bitrate cap); \c joinandconfigure combines the previous
99
 * two requests in a single one (just for publishers); \c publish can be
100
 * used to start sending media to broadcast to the other participants,
101
 * while \c unpublish does the opposite; \c start allows you to start
102
 * receiving media from a publisher you've subscribed to previously by
103
 * means of a \c join , while \c pause pauses the delivery of the media;
104
 * the \c switch request can be used to change the source of the media
105
 * flowing over a specific PeerConnection (e.g., I was watching Alice,
106
 * I want to watch Bob now) without having to create a new handle for
107
 * that; \c stop interrupts a viewer instance; \c add and \c remove
108
 * are just used when involving "Plan B", and are used to add or remove
109
 * publishers to be muxed in the single viewer PeerConnection; finally,
110
 * \c leave allows you to leave a video room for good.
111
 * 
112
 * Notice that, in general, all users can create rooms. If you want to
113
 * limit this functionality, you can configure an admin \c admin_key in
114
 * the plugin settings. When configured, only "create" requests that
115
 * include the correct \c admin_key value in an "admin_key" property
116
 * will succeed, and will be rejected otherwise.
117
 * 
118
 * Actual API docs: TBD.
119
 * 
120
 * \ingroup plugins
121
 * \ref plugins
122
 */
123

    
124
#include "plugin.h"
125

    
126
#include <jansson.h>
127
#include <sofia-sip/sdp.h>
128

    
129
#include "../debug.h"
130
#include "../apierror.h"
131
#include "../config.h"
132
#include "../mutex.h"
133
#include "../rtp.h"
134
#include "../rtcp.h"
135
#include "../record.h"
136
#include "../utils.h"
137
#include <sys/types.h>
138
#include <sys/socket.h>
139

    
140

    
141
/* Plugin information */
142
#define JANUS_VIDEOROOM_VERSION                        7
143
#define JANUS_VIDEOROOM_VERSION_STRING        "0.0.7"
144
#define JANUS_VIDEOROOM_DESCRIPTION                "This is a plugin implementing a videoconferencing SFU (Selective Forwarding Unit) for Janus, that is an audio/video router."
145
#define JANUS_VIDEOROOM_NAME                        "JANUS VideoRoom plugin"
146
#define JANUS_VIDEOROOM_AUTHOR                        "Meetecho s.r.l."
147
#define JANUS_VIDEOROOM_PACKAGE                        "janus.plugin.videoroom"
148

    
149
/* Plugin methods */
150
janus_plugin *create(void);
151
int janus_videoroom_init(janus_callbacks *callback, const char *config_path);
152
void janus_videoroom_destroy(void);
153
int janus_videoroom_get_api_compatibility(void);
154
int janus_videoroom_get_version(void);
155
const char *janus_videoroom_get_version_string(void);
156
const char *janus_videoroom_get_description(void);
157
const char *janus_videoroom_get_name(void);
158
const char *janus_videoroom_get_author(void);
159
const char *janus_videoroom_get_package(void);
160
void janus_videoroom_create_session(janus_plugin_session *handle, int *error);
161
struct janus_plugin_result *janus_videoroom_handle_message(janus_plugin_session *handle, char *transaction, char *message, char *sdp_type, char *sdp);
162
void janus_videoroom_setup_media(janus_plugin_session *handle);
163
void janus_videoroom_incoming_rtp(janus_plugin_session *handle, int video, char *buf, int len);
164
void janus_videoroom_incoming_rtcp(janus_plugin_session *handle, int video, char *buf, int len);
165
void janus_videoroom_incoming_data(janus_plugin_session *handle, char *buf, int len);
166
void janus_videoroom_slow_link(janus_plugin_session *handle, int uplink, int video);
167
void janus_videoroom_hangup_media(janus_plugin_session *handle);
168
void janus_videoroom_destroy_session(janus_plugin_session *handle, int *error);
169
char *janus_videoroom_query_session(janus_plugin_session *handle);
170

    
171
/* Plugin setup */
172
static janus_plugin janus_videoroom_plugin =
173
        JANUS_PLUGIN_INIT (
174
                .init = janus_videoroom_init,
175
                .destroy = janus_videoroom_destroy,
176

    
177
                .get_api_compatibility = janus_videoroom_get_api_compatibility,
178
                .get_version = janus_videoroom_get_version,
179
                .get_version_string = janus_videoroom_get_version_string,
180
                .get_description = janus_videoroom_get_description,
181
                .get_name = janus_videoroom_get_name,
182
                .get_author = janus_videoroom_get_author,
183
                .get_package = janus_videoroom_get_package,
184
                
185
                .create_session = janus_videoroom_create_session,
186
                .handle_message = janus_videoroom_handle_message,
187
                .setup_media = janus_videoroom_setup_media,
188
                .incoming_rtp = janus_videoroom_incoming_rtp,
189
                .incoming_rtcp = janus_videoroom_incoming_rtcp,
190
                .incoming_data = janus_videoroom_incoming_data,
191
                .slow_link = janus_videoroom_slow_link,
192
                .hangup_media = janus_videoroom_hangup_media,
193
                .destroy_session = janus_videoroom_destroy_session,
194
                .query_session = janus_videoroom_query_session,
195
        );
196

    
197
/* Plugin creator */
198
janus_plugin *create(void) {
199
        JANUS_LOG(LOG_VERB, "%s created!\n", JANUS_VIDEOROOM_NAME);
200
        return &janus_videoroom_plugin;
201
}
202

    
203
/* Parameter validation */
204
static struct janus_json_parameter request_parameters[] = {
205
        {"request", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
206
};
207
static struct janus_json_parameter adminkey_parameters[] = {
208
        {"admin_key", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
209
};
210
static struct janus_json_parameter create_parameters[] = {
211
        {"room", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
212
        {"description", JSON_STRING, 0},
213
        {"is_private", JANUS_JSON_BOOL, 0},
214
        {"secret", JSON_STRING, 0},
215
        {"pin", JSON_STRING, 0},
216
        {"bitrate", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
217
        {"fir_freq", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
218
        {"publishers", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
219
        {"audiocodec", JSON_STRING, 0},
220
        {"videocodec", JSON_STRING, 0},
221
        {"record", JANUS_JSON_BOOL, 0},
222
        {"rec_dir", JSON_STRING, 0},
223
        {"permanent", JANUS_JSON_BOOL, 0}
224
};
225
static struct janus_json_parameter room_parameters[] = {
226
        {"room", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE}
227
};
228
static struct janus_json_parameter destroy_parameters[] = {
229
        {"room", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
230
        {"permanent", JANUS_JSON_BOOL, 0}
231
};
232
static struct janus_json_parameter join_parameters[] = {
233
        {"room", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
234
        {"ptype", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
235
        {"audio", JANUS_JSON_BOOL, 0},
236
        {"video", JANUS_JSON_BOOL, 0},
237
        {"bitrate", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
238
        {"record", JANUS_JSON_BOOL, 0},
239
        {"filename", JSON_STRING, 0}
240
};
241
static struct janus_json_parameter publish_parameters[] = {
242
        {"audio", JANUS_JSON_BOOL, 0},
243
        {"video", JANUS_JSON_BOOL, 0},
244
        {"bitrate", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
245
        {"record", JANUS_JSON_BOOL, 0},
246
        {"filename", JSON_STRING, 0}
247
};
248
static struct janus_json_parameter rtp_forward_parameters[] = {
249
        {"room", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
250
        {"publisher_id", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
251
        {"vid_port", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
252
        {"au_port", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
253
        {"host", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
254
};
255
static struct janus_json_parameter stop_rtp_forward_parameters[] = {
256
        {"room", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
257
        {"publisher_id", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
258
        {"stream_id", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE}
259
};
260
static struct janus_json_parameter publisher_parameters[] = {
261
        {"id", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
262
        {"display", JSON_STRING, 0}
263
};
264
static struct janus_json_parameter configure_parameters[] = {
265
        {"audio", JANUS_JSON_BOOL, 0},
266
        {"video", JANUS_JSON_BOOL, 0},
267
        {"data", JANUS_JSON_BOOL, 0}
268
};
269
static struct janus_json_parameter listener_parameters[] = {
270
        {"feed", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
271
        {"audio", JANUS_JSON_BOOL, 0},
272
        {"video", JANUS_JSON_BOOL, 0},
273
        {"data", JANUS_JSON_BOOL, 0}
274
};
275
static struct janus_json_parameter feeds_parameters[] = {
276
        {"feeds", JSON_ARRAY, JANUS_JSON_PARAM_NONEMPTY}
277
};
278

    
279
/* Static configuration instance */
280
static janus_config *config = NULL;
281
static const char *config_folder = NULL;
282
static janus_mutex config_mutex;
283

    
284
/* Useful stuff */
285
static volatile gint initialized = 0, stopping = 0;
286
static gboolean notify_events = TRUE;
287
static janus_callbacks *gateway = NULL;
288
static GThread *handler_thread;
289
static GThread *watchdog;
290
static su_home_t *sdphome = NULL;
291
static void *janus_videoroom_handler(void *data);
292
static void janus_videoroom_relay_rtp_packet(gpointer data, gpointer user_data);
293
static void janus_videoroom_relay_data_packet(gpointer data, gpointer user_data);
294

    
295
typedef enum janus_videoroom_p_type {
296
        janus_videoroom_p_type_none = 0,
297
        janus_videoroom_p_type_subscriber,                        /* Generic listener/subscriber */
298
        janus_videoroom_p_type_subscriber_muxed,        /* Multiplexed listener/subscriber */
299
        janus_videoroom_p_type_publisher,                        /* Participant/publisher */
300
} janus_videoroom_p_type;
301

    
302
typedef struct janus_videoroom_message {
303
        janus_plugin_session *handle;
304
        char *transaction;
305
        json_t *message;
306
        char *sdp_type;
307
        char *sdp;
308
} janus_videoroom_message;
309
static GAsyncQueue *messages = NULL;
310
static janus_videoroom_message exit_message;
311

    
312
static void janus_videoroom_message_free(janus_videoroom_message *msg) {
313
        if(!msg || msg == &exit_message)
314
                return;
315

    
316
        msg->handle = NULL;
317

    
318
        g_free(msg->transaction);
319
        msg->transaction = NULL;
320
        if(msg->message)
321
                json_decref(msg->message);
322
        msg->message = NULL;
323
        g_free(msg->sdp_type);
324
        msg->sdp_type = NULL;
325
        g_free(msg->sdp);
326
        msg->sdp = NULL;
327

    
328
        g_free(msg);
329
}
330

    
331
typedef enum janus_videoroom_audiocodec {
332
        JANUS_VIDEOROOM_OPUS,                /* Publishers will have to use OPUS         */
333
        JANUS_VIDEOROOM_ISAC_32K,        /* Publishers will have to use ISAC 32K */
334
        JANUS_VIDEOROOM_ISAC_16K,        /* Publishers will have to use ISAC 16K */
335
        JANUS_VIDEOROOM_PCMU,                /* Publishers will have to use PCMU 8K         */
336
        JANUS_VIDEOROOM_PCMA                /* Publishers will have to use PCMA 8K         */
337
} janus_videoroom_audiocodec;
338
static const char *janus_videoroom_audiocodec_name(janus_videoroom_audiocodec acodec) {
339
        switch(acodec) {
340
                case JANUS_VIDEOROOM_OPUS:
341
                        return "opus";
342
                case JANUS_VIDEOROOM_ISAC_32K:
343
                        return "isac32";
344
                case JANUS_VIDEOROOM_ISAC_16K:
345
                        return "isac16";
346
                case JANUS_VIDEOROOM_PCMU:
347
                        return "pcmu";
348
                case JANUS_VIDEOROOM_PCMA:
349
                        return "pcma";
350
                default:
351
                        /* Shouldn't happen */
352
                        return "opus";
353
        }
354
}
355

    
356
typedef enum janus_videoroom_videocodec {
357
        JANUS_VIDEOROOM_VP8,        /* Publishers will have to use VP8 */
358
        JANUS_VIDEOROOM_VP9,        /* Publishers will have to use VP9 */
359
        JANUS_VIDEOROOM_H264        /* Publishers will have to use H264 */
360
} janus_videoroom_videocodec;
361
static const char *janus_videoroom_videocodec_name(janus_videoroom_videocodec vcodec) {
362
        switch(vcodec) {
363
                case JANUS_VIDEOROOM_VP8:
364
                        return "vp8";
365
                case JANUS_VIDEOROOM_VP9:
366
                        return "vp9";
367
                case JANUS_VIDEOROOM_H264:
368
                        return "h264";
369
                default:
370
                        /* Shouldn't happen */
371
                        return "vp8";
372
        }
373
}
374

    
375
typedef struct janus_videoroom {
376
        guint64 room_id;                        /* Unique room ID */
377
        gchar *room_name;                        /* Room description */
378
        gchar *room_secret;                        /* Secret needed to manipulate (e.g., destroy) this room */
379
        gchar *room_pin;                        /* Password needed to join this room, if any */
380
        gboolean is_private;                /* Whether this room is 'private' (as in hidden) or not */
381
        int max_publishers;                        /* Maximum number of concurrent publishers */
382
        uint64_t bitrate;                        /* Global bitrate limit */
383
        uint16_t fir_freq;                        /* Regular FIR frequency (0=disabled) */
384
        janus_videoroom_audiocodec acodec;        /* Audio codec to force on publishers*/
385
        janus_videoroom_videocodec vcodec;        /* Video codec to force on publishers*/
386
        gboolean record;                        /* Whether the feeds from publishers in this room should be recorded */
387
        char *rec_dir;                                /* Where to save the recordings of this room, if enabled */
388
        gint64 destroyed;                        /* Value to flag the room for destruction, done lazily */
389
        GHashTable *participants;        /* Map of potential publishers (we get listeners from them) */
390
        janus_mutex participants_mutex;/* Mutex to protect room properties */
391
} janus_videoroom;
392
static GHashTable *rooms;
393
static janus_mutex rooms_mutex;
394
static GList *old_rooms;
395
static char *admin_key = NULL;
396
static void janus_videoroom_free(janus_videoroom *room);
397

    
398
typedef struct janus_videoroom_session {
399
        janus_plugin_session *handle;
400
        janus_videoroom_p_type participant_type;
401
        gpointer participant;
402
        gboolean started;
403
        gboolean stopping;
404
        volatile gint hangingup;
405
        gint64 destroyed;        /* Time at which this session was marked as destroyed */
406
} janus_videoroom_session;
407
static GHashTable *sessions;
408
static GList *old_sessions;
409
static janus_mutex sessions_mutex;
410

    
411
/* a host whose ports gets streamed rtp packets of the corresponding type. */
412
typedef struct rtp_forwarder {
413
        int is_video;
414
        struct sockaddr_in serv_addr;
415
} rtp_forwarder;
416

    
417
typedef struct janus_videoroom_participant {
418
        janus_videoroom_session *session;
419
        janus_videoroom *room;        /* Room */
420
        guint64 user_id;        /* Unique ID in the room */
421
        gchar *display;        /* Display name (just for fun) */
422
        gchar *sdp;                        /* The SDP this publisher negotiated, if any */
423
        gboolean audio, video, data;                /* Whether audio, video and/or data is going to be sent by this publisher */
424
        guint32 audio_pt;                /* Audio payload type (Opus) */
425
        guint32 video_pt;                /* Video payload type (depends on room configuration) */
426
        guint32 audio_ssrc;                /* Audio SSRC of this publisher */
427
        guint32 video_ssrc;                /* Video SSRC of this publisher */
428
        gboolean audio_active;
429
        gboolean video_active;
430
        gboolean firefox;        /* We send Firefox users a different kind of FIR */
431
        uint64_t bitrate;
432
        gint64 remb_startup;/* Incremental changes on REMB to reach the target at startup */
433
        gint64 remb_latest;        /* Time of latest sent REMB (to avoid flooding) */
434
        gint64 fir_latest;        /* Time of latest sent FIR (to avoid flooding) */
435
        gint fir_seq;                /* FIR sequence number */
436
        gboolean recording_active;        /* Whether this publisher has to be recorded or not */
437
        gchar *recording_base;        /* Base name for the recording (e.g., /path/to/filename, will generate /path/to/filename-audio.mjr and/or /path/to/filename-video.mjr */
438
        janus_recorder *arc;        /* The Janus recorder instance for this publisher's audio, if enabled */
439
        janus_recorder *vrc;        /* The Janus recorder instance for this publisher's video, if enabled */
440
        janus_mutex rec_mutex;        /* Mutex to protect the recorders from race conditions */
441
        GSList *listeners;
442
        janus_mutex listeners_mutex;
443
        GHashTable *rtp_forwarders;
444
        janus_mutex rtp_forwarders_mutex;
445
        int udp_sock; /* The udp socket on which to forward rtp packets */
446
} janus_videoroom_participant;
447
static void janus_videoroom_participant_free(janus_videoroom_participant *p);
448
static void janus_rtp_forwarder_free_helper(gpointer data);
449
static guint32 janus_rtp_forwarder_add_helper(janus_videoroom_participant *p, const gchar* host, int port, int is_video);
450
typedef struct janus_videoroom_listener_context {
451
        /* Needed to fix seq and ts in case of publisher switching */
452
        uint32_t a_last_ssrc, a_last_ts, a_base_ts, a_base_ts_prev,
453
                        v_last_ssrc, v_last_ts, v_base_ts, v_base_ts_prev;
454
        uint16_t a_last_seq, a_base_seq, a_base_seq_prev,
455
                        v_last_seq, v_base_seq, v_base_seq_prev;
456
} janus_videoroom_listener_context;
457

    
458
typedef struct janus_videoroom_listener {
459
        janus_videoroom_session *session;
460
        janus_videoroom *room;        /* Room */
461
        janus_videoroom_participant *feed;        /* Participant this listener is subscribed to */
462
        janus_videoroom_listener_context context;        /* Needed in case there are publisher switches on this listener */
463
        gboolean audio, video, data;                /* Whether audio, video and/or data must be sent to this publisher */
464
        struct janus_videoroom_listener_muxed *parent;        /* Overall subscriber, if this is a sub-listener in a Multiplexed one */
465
        gboolean paused;
466
} janus_videoroom_listener;
467
static void janus_videoroom_listener_free(janus_videoroom_listener *l);
468

    
469
typedef struct janus_videoroom_listener_muxed {
470
        janus_videoroom_session *session;
471
        janus_videoroom *room;        /* Room */
472
        GSList *listeners;        /* List of listeners (as a Multiplexed listener can be subscribed to more publishers at the same time) */
473
        janus_mutex listeners_mutex;
474
} janus_videoroom_listener_muxed;
475
static void janus_videoroom_muxed_listener_free(janus_videoroom_listener_muxed *l);
476

    
477
typedef struct janus_videoroom_rtp_relay_packet {
478
        rtp_header *data;
479
        gint length;
480
        gint is_video;
481
        uint32_t timestamp;
482
        uint16_t seq_number;
483
} janus_videoroom_rtp_relay_packet;
484

    
485
/* SDP offer/answer templates */
486
#define OPUS_PT        111
487
#define ISAC32_PT        104
488
#define ISAC16_PT        103
489
#define PCMU_PT        0
490
#define PCMA_PT        8
491
#define VP8_PT                100
492
#define VP9_PT                101
493
#define H264_PT        107
494
#define sdp_template \
495
                "v=0\r\n" \
496
                "o=- %"SCNu64" %"SCNu64" IN IP4 127.0.0.1\r\n"        /* We need current time here */ \
497
                "s=%s\r\n"                                                        /* Video room name */ \
498
                "t=0 0\r\n" \
499
                "%s%s%s"                                /* Audio, video and/or data channel m-lines */
500
#define sdp_a_template_opus \
501
                "m=audio 1 RTP/SAVPF %d\r\n"                /* Opus payload type */ \
502
                "c=IN IP4 1.1.1.1\r\n" \
503
                "a=%s\r\n"                                                        /* Media direction */ \
504
                "a=rtpmap:%d opus/48000/2\r\n"                /* Opus payload type */
505
#define sdp_a_template_isac32 \
506
                "m=audio 1 RTP/SAVPF %d\r\n"                /* ISAC32_PT payload type */ \
507
                "c=IN IP4 1.1.1.1\r\n" \
508
                "a=%s\r\n"                                                        /* Media direction */ \
509
                "a=rtpmap:%d ISAC/32000\r\n"                /* ISAC32_PT payload type */
510
#define sdp_a_template_isac16 \
511
                "m=audio 1 RTP/SAVPF %d\r\n"                /* ISAC16_PT payload type */ \
512
                "c=IN IP4 1.1.1.1\r\n" \
513
                "a=%s\r\n"                                                        /* Media direction */ \
514
                "a=rtpmap:%d ISAC/16000\r\n"                /* ISAC16_PT payload type */
515
#define sdp_a_template_pcmu \
516
                "m=audio 1 RTP/SAVPF %d\r\n"                /* PCMU_PT payload type */ \
517
                "c=IN IP4 1.1.1.1\r\n" \
518
                "a=%s\r\n"                                                        /* Media direction */ \
519
                "a=rtpmap:%d PCMU/8000\r\n"                    /* PCMU_PT payload type */
520
#define sdp_a_template_pcma \
521
                "m=audio 1 RTP/SAVPF %d\r\n"                /* PCMA_PT payload type */ \
522
                "c=IN IP4 1.1.1.1\r\n" \
523
                "a=%s\r\n"                                                        /* Media direction */ \
524
                "a=rtpmap:%d PCMA/8000\r\n"                    /* PCMA_PT payload type */
525
#define sdp_v_template_vp8 \
526
                "m=video 1 RTP/SAVPF %d\r\n"                /* VP8 payload type */ \
527
                "c=IN IP4 1.1.1.1\r\n" \
528
                "b=AS:%d\r\n"                                                /* Bandwidth */ \
529
                "a=%s\r\n"                                                        /* Media direction */ \
530
                "a=rtpmap:%d VP8/90000\r\n"                        /* VP8 payload type */ \
531
                "a=rtcp-fb:%d ccm fir\r\n"                        /* VP8 payload type */ \
532
                "a=rtcp-fb:%d nack\r\n"                                /* VP8 payload type */ \
533
                "a=rtcp-fb:%d nack pli\r\n"                        /* VP8 payload type */ \
534
                "a=rtcp-fb:%d goog-remb\r\n"                /* VP8 payload type */
535
#define sdp_v_template_vp9 \
536
                "m=video 1 RTP/SAVPF %d\r\n"                /* VP9 payload type */ \
537
                "c=IN IP4 1.1.1.1\r\n" \
538
                "b=AS:%d\r\n"                                                /* Bandwidth */ \
539
                "a=%s\r\n"                                                        /* Media direction */ \
540
                "a=rtpmap:%d VP9/90000\r\n"                        /* VP9 payload type */ \
541
                "a=rtcp-fb:%d ccm fir\r\n"                        /* VP9 payload type */ \
542
                "a=rtcp-fb:%d nack\r\n"                                /* VP9 payload type */ \
543
                "a=rtcp-fb:%d nack pli\r\n"                        /* VP9 payload type */ \
544
                "a=rtcp-fb:%d goog-remb\r\n"                /* VP9 payload type */
545
#define sdp_v_template_h264 \
546
                "m=video 1 RTP/SAVPF %d\r\n"                /* H264 payload type */ \
547
                "c=IN IP4 1.1.1.1\r\n" \
548
                "b=AS:%d\r\n"                                                /* Bandwidth */ \
549
                "a=%s\r\n"                                                        /* Media direction */ \
550
                "a=rtpmap:%d H264/90000\r\n"                /* H264 payload type */ \
551
                "a=fmtp:%d profile-level-id=42e01f;packetization-mode=1\r\n" \
552
                "a=rtcp-fb:%d ccm fir\r\n"                        /* H264 payload type */ \
553
                "a=rtcp-fb:%d nack\r\n"                                /* H264 payload type */ \
554
                "a=rtcp-fb:%d nack pli\r\n"                        /* H264 payload type */ \
555
                "a=rtcp-fb:%d goog-remb\r\n"                /* H264 payload type */
556
#define sdp_d_template \
557
                "m=application 1 DTLS/SCTP 5000\r\n" \
558
                "c=IN IP4 1.1.1.1\r\n" \
559
                "a=sctpmap:5000 webrtc-datachannel 16\r\n"
560

    
561

    
562
/* Error codes */
563
#define JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR                499
564
#define JANUS_VIDEOROOM_ERROR_NO_MESSAGE                421
565
#define JANUS_VIDEOROOM_ERROR_INVALID_JSON                422
566
#define JANUS_VIDEOROOM_ERROR_INVALID_REQUEST        423
567
#define JANUS_VIDEOROOM_ERROR_JOIN_FIRST                424
568
#define JANUS_VIDEOROOM_ERROR_ALREADY_JOINED        425
569
#define JANUS_VIDEOROOM_ERROR_NO_SUCH_ROOM                426
570
#define JANUS_VIDEOROOM_ERROR_ROOM_EXISTS                427
571
#define JANUS_VIDEOROOM_ERROR_NO_SUCH_FEED                428
572
#define JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT        429
573
#define JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT        430
574
#define JANUS_VIDEOROOM_ERROR_INVALID_SDP_TYPE        431
575
#define JANUS_VIDEOROOM_ERROR_PUBLISHERS_FULL        432
576
#define JANUS_VIDEOROOM_ERROR_UNAUTHORIZED                433
577
#define JANUS_VIDEOROOM_ERROR_ALREADY_PUBLISHED        434
578
#define JANUS_VIDEOROOM_ERROR_NOT_PUBLISHED                435
579
#define JANUS_VIDEOROOM_ERROR_ID_EXISTS                        436
580
#define JANUS_VIDEOROOM_ERROR_INVALID_SDP                437
581

    
582

    
583
/* Multiplexing helpers */
584
int janus_videoroom_muxed_subscribe(janus_videoroom_listener_muxed *muxed_listener, GList *feeds, char *transaction);
585
int janus_videoroom_muxed_unsubscribe(janus_videoroom_listener_muxed *muxed_listener, GList *feeds, char *transaction);
586
int janus_videoroom_muxed_offer(janus_videoroom_listener_muxed *muxed_listener, char *transaction, char *event_text);
587

    
588
static guint32 janus_rtp_forwarder_add_helper(janus_videoroom_participant *p, const gchar* host, int port, int is_video) {
589
        if(!p || !host) {
590
                return 0;
591
        }
592
        rtp_forwarder *forward = g_malloc0(sizeof(rtp_forwarder));
593
        forward->is_video = is_video;
594
        forward->serv_addr.sin_family = AF_INET;
595
        inet_pton(AF_INET, host, &(forward->serv_addr.sin_addr));
596
        forward->serv_addr.sin_port = htons(port);
597
        janus_mutex_lock(&p->rtp_forwarders_mutex);
598
        guint32 stream_id = janus_random_uint32();
599
        while(g_hash_table_lookup(p->rtp_forwarders, GUINT_TO_POINTER(stream_id)) != NULL) {
600
                stream_id = janus_random_uint32();
601
        }
602
        g_hash_table_insert(p->rtp_forwarders, GUINT_TO_POINTER(stream_id), forward);
603
        janus_mutex_unlock(&p->rtp_forwarders_mutex);
604
        JANUS_LOG(LOG_VERB, "Added %s rtp_forward to participant %"SCNu64" host: %s:%d stream_id: %"SCNu32"\n", is_video ? "video":"audio", p->user_id, host, port, stream_id);
605
        return stream_id;
606
}
607

    
608

    
609
/* Convenience function for freeing a session */
610
static void session_free(gpointer data) {
611
        if(data) {
612
                janus_videoroom_session* session = (janus_videoroom_session*)data;
613
                switch(session->participant_type) {
614
                case janus_videoroom_p_type_publisher: 
615
                        janus_videoroom_participant_free(session->participant);
616
                        break;   
617
                case janus_videoroom_p_type_subscriber:
618
                        janus_videoroom_listener_free(session->participant);
619
                        break;
620
                case janus_videoroom_p_type_subscriber_muxed:
621
                        janus_videoroom_muxed_listener_free(session->participant);
622
                        break;
623
                default:
624
                        break;
625
                }
626
                session->handle = NULL;
627
                g_free(session);
628
                session = NULL;
629
        }
630
}
631

    
632
static void janus_rtp_forwarder_free_helper(gpointer data) {
633
        if(data) {
634
                rtp_forwarder* forward = (rtp_forwarder*)data;
635
                if(forward) {
636
                        g_free(forward);
637
                        forward = NULL;
638
                }
639
        }
640
}
641

    
642
/* Convenience wrapper function for session_free that corresponds to GHRFunc() format for hash table cleanup */
643
static gboolean session_hash_table_remove(gpointer key, gpointer value, gpointer not_used) {
644
        if(value) {
645
                session_free(value);
646
        }
647
        return TRUE;
648
}
649

    
650
/* VideoRoom watchdog/garbage collector (sort of) */
651
void *janus_videoroom_watchdog(void *data);
652
void *janus_videoroom_watchdog(void *data) {
653
        JANUS_LOG(LOG_INFO, "VideoRoom watchdog started\n");
654
        gint64 now = 0, room_now = 0;
655
        while(g_atomic_int_get(&initialized) && !g_atomic_int_get(&stopping)) {
656
                janus_mutex_lock(&sessions_mutex);
657
                /* Iterate on all the participants/listeners and check if we need to remove any of them */
658
                now = janus_get_monotonic_time();
659
                if(old_sessions != NULL) {
660
                        GList *sl = old_sessions;
661
                        JANUS_LOG(LOG_HUGE, "Checking %d old VideoRoom sessions...\n", g_list_length(old_sessions));
662
                        while(sl) {
663
                                janus_videoroom_session *session = (janus_videoroom_session *)sl->data;
664
                                /* If we are stopping, their is no point to continue to iterate */
665
                                if(!initialized || stopping) {
666
                                        break;
667
                                }
668
                                if(!session) {
669
                                        sl = sl->next;
670
                                        continue;
671
                                }
672
                                if(now-session->destroyed >= 5*G_USEC_PER_SEC) {
673
                                        /* We're lazy and actually get rid of the stuff only after a few seconds */
674
                                        JANUS_LOG(LOG_VERB, "Freeing old VideoRoom session\n");
675
                                        GList *rm = sl->next;
676
                                        old_sessions = g_list_delete_link(old_sessions, sl);
677
                                        sl = rm;
678
                                        g_hash_table_steal(sessions, session->handle);
679
                                        session_free(session);
680
                                        continue;
681
                                }
682
                                sl = sl->next;
683
                        }
684
                }
685
                janus_mutex_unlock(&sessions_mutex);
686
                janus_mutex_lock(&rooms_mutex);
687
                if(old_rooms != NULL) {
688
                        GList *rl = old_rooms;
689
                        room_now = janus_get_monotonic_time();
690
                        while(rl) {
691
                                janus_videoroom* room = (janus_videoroom*)rl->data;
692
                                if(!initialized || stopping){
693
                                        break;
694
                                }
695
                                if(!room) {
696
                                        rl = rl->next;
697
                                        continue;
698
                                }
699
                                if(room_now - room->destroyed >= 5*G_USEC_PER_SEC) {
700
                                        GList *rm = rl->next;
701
                                        old_rooms = g_list_delete_link(old_rooms, rl);
702
                                        rl = rm;
703
                                        g_hash_table_remove(rooms, &room->room_id);
704
                                        continue;
705
                                }
706
                                rl = rl->next;
707
                        }
708
                }
709
                janus_mutex_unlock(&rooms_mutex);
710
                g_usleep(500000);
711
        }
712
        JANUS_LOG(LOG_INFO, "VideoRoom watchdog stopped\n");
713
        return NULL;
714
}
715

    
716

    
717
/* Plugin implementation */
718
int janus_videoroom_init(janus_callbacks *callback, const char *config_path) {
719
        if(g_atomic_int_get(&stopping)) {
720
                /* Still stopping from before */
721
                return -1;
722
        }
723
        if(callback == NULL || config_path == NULL) {
724
                /* Invalid arguments */
725
                return -1;
726
        }
727
        sdphome = su_home_new(sizeof(su_home_t));
728
        if(su_home_init(sdphome) < 0) {
729
                JANUS_LOG(LOG_FATAL, "Ops, error setting up sofia-sdp?\n");
730
                return -1;
731
        }
732

    
733
        /* Read configuration */
734
        char filename[255];
735
        g_snprintf(filename, 255, "%s/%s.cfg", config_path, JANUS_VIDEOROOM_PACKAGE);
736
        JANUS_LOG(LOG_VERB, "Configuration file: %s\n", filename);
737
        config = janus_config_parse(filename);
738
        config_folder = config_path;
739
        if(config != NULL)
740
                janus_config_print(config);
741
        janus_mutex_init(&config_mutex);
742

    
743
        rooms = g_hash_table_new_full(g_int64_hash, g_int64_equal,
744
                (GDestroyNotify)g_free, (GDestroyNotify) janus_videoroom_free);
745
        janus_mutex_init(&rooms_mutex);
746
        sessions = g_hash_table_new(NULL, NULL);
747
        janus_mutex_init(&sessions_mutex);
748

    
749
        messages = g_async_queue_new_full((GDestroyNotify) janus_videoroom_message_free);
750

    
751
        /* This is the callback we'll need to invoke to contact the gateway */
752
        gateway = callback;
753

    
754
        /* Parse configuration to populate the rooms list */
755
        if(config != NULL) {
756
                /* Any admin key to limit who can "create"? */
757
                janus_config_item *key = janus_config_get_item_drilldown(config, "general", "admin_key");
758
                if(key != NULL && key->value != NULL)
759
                        admin_key = g_strdup(key->value);
760
                janus_config_item *events = janus_config_get_item_drilldown(config, "general", "events");
761
                if(events != NULL && events->value != NULL)
762
                        notify_events = janus_is_true(events->value);
763
                if(!notify_events && callback->events_is_enabled()) {
764
                        JANUS_LOG(LOG_WARN, "Notification of events to handlers disabled for %s\n", JANUS_VIDEOROOM_NAME);
765
                }
766
                /* Iterate on all rooms */
767
                GList *cl = janus_config_get_categories(config);
768
                while(cl != NULL) {
769
                        janus_config_category *cat = (janus_config_category *)cl->data;
770
                        if(cat->name == NULL || !strcasecmp(cat->name, "general")) {
771
                                cl = cl->next;
772
                                continue;
773
                        }
774
                        JANUS_LOG(LOG_VERB, "Adding video room '%s'\n", cat->name);
775
                        janus_config_item *desc = janus_config_get_item(cat, "description");
776
                        janus_config_item *priv = janus_config_get_item(cat, "is_private");
777
                        janus_config_item *secret = janus_config_get_item(cat, "secret");
778
                        janus_config_item *pin = janus_config_get_item(cat, "pin");
779
                        janus_config_item *bitrate = janus_config_get_item(cat, "bitrate");
780
                        janus_config_item *maxp = janus_config_get_item(cat, "publishers");
781
                        janus_config_item *firfreq = janus_config_get_item(cat, "fir_freq");
782
                        janus_config_item *audiocodec = janus_config_get_item(cat, "audiocodec");
783
                        janus_config_item *videocodec = janus_config_get_item(cat, "videocodec");
784
                        janus_config_item *record = janus_config_get_item(cat, "record");
785
                        janus_config_item *rec_dir = janus_config_get_item(cat, "rec_dir");
786
                        /* Create the video room */
787
                        janus_videoroom *videoroom = g_malloc0(sizeof(janus_videoroom));
788
                        if(videoroom == NULL) {
789
                                JANUS_LOG(LOG_FATAL, "Memory error!\n");
790
                                continue;
791
                        }
792
                        videoroom->room_id = atol(cat->name);
793
                        char *description = NULL;
794
                        if(desc != NULL && desc->value != NULL && strlen(desc->value) > 0)
795
                                description = g_strdup(desc->value);
796
                        else
797
                                description = g_strdup(cat->name);
798
                        if(description == NULL) {
799
                                JANUS_LOG(LOG_FATAL, "Memory error!\n");
800
                                continue;
801
                        }
802
                        videoroom->room_name = description;
803
                        if(secret != NULL && secret->value != NULL) {
804
                                videoroom->room_secret = g_strdup(secret->value);
805
                        }
806
                        if(pin != NULL && pin->value != NULL) {
807
                                videoroom->room_pin = g_strdup(pin->value);
808
                        }
809
                        videoroom->is_private = priv && priv->value && janus_is_true(priv->value);
810
                        videoroom->max_publishers = 3;        /* FIXME How should we choose a default? */
811
                        if(maxp != NULL && maxp->value != NULL)
812
                                videoroom->max_publishers = atol(maxp->value);
813
                        if(videoroom->max_publishers < 0)
814
                                videoroom->max_publishers = 3;        /* FIXME How should we choose a default? */
815
                        videoroom->bitrate = 0;
816
                        if(bitrate != NULL && bitrate->value != NULL)
817
                                videoroom->bitrate = atol(bitrate->value);
818
                        if(videoroom->bitrate > 0 && videoroom->bitrate < 64000)
819
                                videoroom->bitrate = 64000;        /* Don't go below 64k */
820
                        videoroom->fir_freq = 0;
821
                        if(firfreq != NULL && firfreq->value != NULL)
822
                                videoroom->fir_freq = atol(firfreq->value);
823
                        videoroom->acodec = JANUS_VIDEOROOM_OPUS;
824
                        if(audiocodec && audiocodec->value) {
825
                                if(!strcasecmp(audiocodec->value, "opus"))
826
                                        videoroom->acodec = JANUS_VIDEOROOM_OPUS;
827
                                else if(!strcasecmp(audiocodec->value, "isac32"))
828
                                        videoroom->acodec = JANUS_VIDEOROOM_ISAC_32K;
829
                                else if(!strcasecmp(audiocodec->value, "isac16"))
830
                                        videoroom->acodec = JANUS_VIDEOROOM_ISAC_16K;
831
                                else if(!strcasecmp(audiocodec->value, "pcmu"))
832
                                        videoroom->acodec = JANUS_VIDEOROOM_PCMU;
833
                                else if(!strcasecmp(audiocodec->value, "pcma"))
834
                                        videoroom->acodec = JANUS_VIDEOROOM_PCMA;
835
                                else {
836
                                        JANUS_LOG(LOG_WARN, "Unsupported audio codec '%s', falling back to OPUS\n", audiocodec->value);
837
                                        videoroom->acodec = JANUS_VIDEOROOM_OPUS;
838
                                }
839
                        }
840
                        videoroom->vcodec = JANUS_VIDEOROOM_VP8;
841
                        if(videocodec && videocodec->value) {
842
                                if(!strcasecmp(videocodec->value, "vp8"))
843
                                        videoroom->vcodec = JANUS_VIDEOROOM_VP8;
844
                                else if(!strcasecmp(videocodec->value, "vp9"))
845
                                        videoroom->vcodec = JANUS_VIDEOROOM_VP9;
846
                                else if(!strcasecmp(videocodec->value, "h264"))
847
                                        videoroom->vcodec = JANUS_VIDEOROOM_H264;
848
                                else {
849
                                        JANUS_LOG(LOG_WARN, "Unsupported video codec '%s', falling back to VP8\n", videocodec->value);
850
                                        videoroom->vcodec = JANUS_VIDEOROOM_VP8;
851
                                }
852
                        }
853
                        if(record && record->value) {
854
                                videoroom->record = janus_is_true(record->value);
855
                        }
856
                        if(rec_dir && rec_dir->value) {
857
                                videoroom->rec_dir = g_strdup(rec_dir->value);
858
                        }
859
                        videoroom->destroyed = 0;
860
                        janus_mutex_init(&videoroom->participants_mutex);
861
                        videoroom->participants = g_hash_table_new_full(g_int64_hash, g_int64_equal, (GDestroyNotify)g_free, NULL);
862
                        janus_mutex_lock(&rooms_mutex);
863
                        g_hash_table_insert(rooms, janus_uint64_dup(videoroom->room_id), videoroom);
864
                        janus_mutex_unlock(&rooms_mutex);
865
                        JANUS_LOG(LOG_VERB, "Created videoroom: %"SCNu64" (%s, %s, %s/%s codecs, secret: %s, pin: %s)\n",
866
                                videoroom->room_id, videoroom->room_name,
867
                                videoroom->is_private ? "private" : "public",
868
                                janus_videoroom_audiocodec_name(videoroom->acodec),
869
                                janus_videoroom_videocodec_name(videoroom->vcodec),
870
                                videoroom->room_secret ? videoroom->room_secret : "no secret",
871
                                videoroom->room_pin ? videoroom->room_pin : "no pin");
872
                        if(videoroom->record) {
873
                                JANUS_LOG(LOG_VERB, "  -- Room is going to be recorded in %s\n", videoroom->rec_dir ? videoroom->rec_dir : "the current folder");
874
                        }
875
                        cl = cl->next;
876
                }
877
                /* Done: we keep the configuration file open in case we get a "create" or "destroy" with permanent=true */
878
        }
879

    
880
        /* Show available rooms */
881
        janus_mutex_lock(&rooms_mutex);
882
        GHashTableIter iter;
883
        gpointer value;
884
        g_hash_table_iter_init(&iter, rooms);
885
        while (g_hash_table_iter_next(&iter, NULL, &value)) {
886
                janus_videoroom *vr = value;
887
                JANUS_LOG(LOG_VERB, "  ::: [%"SCNu64"][%s] %"SCNu64", max %d publishers, FIR frequency of %d seconds, %s audio codec, %s video codec\n",
888
                        vr->room_id, vr->room_name, vr->bitrate, vr->max_publishers, vr->fir_freq,
889
                        janus_videoroom_audiocodec_name(vr->acodec), janus_videoroom_videocodec_name(vr->vcodec));
890
        }
891
        janus_mutex_unlock(&rooms_mutex);
892

    
893
        g_atomic_int_set(&initialized, 1);
894

    
895
        GError *error = NULL;
896
        /* Start the sessions watchdog */
897
        watchdog = g_thread_try_new("vroom watchdog", &janus_videoroom_watchdog, NULL, &error);
898
        if(error != NULL) {
899
                g_atomic_int_set(&initialized, 0);
900
                JANUS_LOG(LOG_ERR, "Got error %d (%s) trying to launch the VideoRoom watchdog thread...\n", error->code, error->message ? error->message : "??");
901
                janus_config_destroy(config);
902
                return -1;
903
        }
904
        /* Launch the thread that will handle incoming messages */
905
        handler_thread = g_thread_try_new("janus videoroom handler", janus_videoroom_handler, NULL, &error);
906
        if(error != NULL) {
907
                g_atomic_int_set(&initialized, 0);
908
                JANUS_LOG(LOG_ERR, "Got error %d (%s) trying to launch the VideoRoom handler thread...\n", error->code, error->message ? error->message : "??");
909
                janus_config_destroy(config);
910
                return -1;
911
        }
912
        JANUS_LOG(LOG_INFO, "%s initialized!\n", JANUS_VIDEOROOM_NAME);
913
        return 0;
914
}
915

    
916
void janus_videoroom_destroy(void) {
917
        if(!g_atomic_int_get(&initialized))
918
                return;
919
        g_atomic_int_set(&stopping, 1);
920

    
921
        g_async_queue_push(messages, &exit_message);
922
        if(handler_thread != NULL) {
923
                g_thread_join(handler_thread);
924
                handler_thread = NULL;
925
        }
926
        if(watchdog != NULL) {
927
                g_thread_join(watchdog);
928
                watchdog = NULL;
929
        }
930
        su_home_deinit(sdphome);
931
        su_home_unref(sdphome);
932
        sdphome = NULL;
933

    
934
        /* FIXME We should destroy the sessions cleanly */
935
        janus_mutex_lock(&sessions_mutex);
936
        g_hash_table_foreach_remove(sessions, (GHRFunc)session_hash_table_remove, NULL);
937
        g_hash_table_destroy(sessions);
938
        sessions = NULL;
939
        janus_mutex_unlock(&sessions_mutex);
940

    
941
        janus_mutex_lock(&rooms_mutex);
942

    
943
        g_hash_table_destroy(rooms);
944
        rooms = NULL;
945
        janus_mutex_unlock(&rooms_mutex);
946
        janus_mutex_destroy(&rooms_mutex);
947

    
948
        g_async_queue_unref(messages);
949
        messages = NULL;
950

    
951
        janus_config_destroy(config);
952
        g_free(admin_key);
953

    
954
        g_atomic_int_set(&initialized, 0);
955
        g_atomic_int_set(&stopping, 0);
956
        JANUS_LOG(LOG_INFO, "%s destroyed!\n", JANUS_VIDEOROOM_NAME);
957
}
958

    
959
int janus_videoroom_get_api_compatibility(void) {
960
        /* Important! This is what your plugin MUST always return: don't lie here or bad things will happen */
961
        return JANUS_PLUGIN_API_VERSION;
962
}
963

    
964
int janus_videoroom_get_version(void) {
965
        return JANUS_VIDEOROOM_VERSION;
966
}
967

    
968
const char *janus_videoroom_get_version_string(void) {
969
        return JANUS_VIDEOROOM_VERSION_STRING;
970
}
971

    
972
const char *janus_videoroom_get_description(void) {
973
        return JANUS_VIDEOROOM_DESCRIPTION;
974
}
975

    
976
const char *janus_videoroom_get_name(void) {
977
        return JANUS_VIDEOROOM_NAME;
978
}
979

    
980
const char *janus_videoroom_get_author(void) {
981
        return JANUS_VIDEOROOM_AUTHOR;
982
}
983

    
984
const char *janus_videoroom_get_package(void) {
985
        return JANUS_VIDEOROOM_PACKAGE;
986
}
987

    
988
void janus_videoroom_create_session(janus_plugin_session *handle, int *error) {
989
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized)) {
990
                *error = -1;
991
                return;
992
        }        
993
        janus_videoroom_session *session = (janus_videoroom_session *)g_malloc0(sizeof(janus_videoroom_session));
994
        if(session == NULL) {
995
                JANUS_LOG(LOG_FATAL, "Memory error!\n");
996
                *error = -2;
997
                return;
998
        }
999
        session->handle = handle;
1000
        session->participant_type = janus_videoroom_p_type_none;
1001
        session->participant = NULL;
1002
        session->destroyed = 0;
1003
        g_atomic_int_set(&session->hangingup, 0);
1004
        handle->plugin_handle = session;
1005
        janus_mutex_lock(&sessions_mutex);
1006
        g_hash_table_insert(sessions, handle, session);
1007
        janus_mutex_unlock(&sessions_mutex);
1008

    
1009
        return;
1010
}
1011

    
1012
static void janus_videoroom_notify_participants(janus_videoroom_participant *participant, char *msg) {
1013
        /* participant->room->participants_mutex has to be locked. */
1014
        GHashTableIter iter;
1015
        gpointer value;
1016
        g_hash_table_iter_init(&iter, participant->room->participants);
1017
        while (!participant->room->destroyed && g_hash_table_iter_next(&iter, NULL, &value)) {
1018
                janus_videoroom_participant *p = value;
1019
                if(p && p->session && p != participant) {
1020
                        JANUS_LOG(LOG_VERB, "Notifying participant %"SCNu64" (%s)\n", p->user_id, p->display ? p->display : "??");
1021
                        int ret = gateway->push_event(p->session->handle, &janus_videoroom_plugin, NULL, msg, NULL, NULL);
1022
                        JANUS_LOG(LOG_VERB, "  >> %d (%s)\n", ret, janus_get_api_error(ret));
1023
                }
1024
        }
1025
}
1026

    
1027
static void janus_videoroom_leave_or_unpublish(janus_videoroom_participant *participant, gboolean is_leaving) {
1028
        json_t *event = json_object();
1029
        json_object_set_new(event, "videoroom", json_string("event"));
1030
        json_object_set_new(event, "room", json_integer(participant->room->room_id));
1031
        json_object_set_new(event, is_leaving ? "leaving" : "unpublished", json_integer(participant->user_id));
1032
        char *leaving_text = json_dumps(event, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1033
        json_decref(event);
1034
        /* we need to check if the room still exists, may have been destroyed already */
1035
        if(participant->room) {
1036
                if(!participant->room->destroyed) {
1037
                        janus_mutex_lock(&participant->room->participants_mutex);
1038
                        janus_videoroom_notify_participants(participant, leaving_text);
1039
                        if(is_leaving) {
1040
                                g_hash_table_remove(participant->room->participants, &participant->user_id);
1041
                        }
1042
                        janus_mutex_unlock(&participant->room->participants_mutex);
1043
                }
1044
        }
1045
        g_free(leaving_text);
1046
}
1047

    
1048
void janus_videoroom_destroy_session(janus_plugin_session *handle, int *error) {
1049
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized)) {
1050
                *error = -1;
1051
                return;
1052
        }        
1053
        janus_videoroom_session *session = (janus_videoroom_session *)handle->plugin_handle; 
1054
        if(!session) {
1055
                JANUS_LOG(LOG_ERR, "No VideoRoom session associated with this handle...\n");
1056
                *error = -2;
1057
                return;
1058
        }
1059
        if(session->destroyed) {
1060
                JANUS_LOG(LOG_WARN, "VideoRoom session already marked as destroyed...\n");
1061
                return;
1062
        }
1063
        JANUS_LOG(LOG_VERB, "Removing VideoRoom session...\n");
1064
        /* Cleaning up and removing the session is done in a lazy way */
1065
        janus_mutex_lock(&sessions_mutex);
1066
        if(!session->destroyed) {
1067
                /* Any related WebRTC PeerConnection is not available anymore either */
1068
                janus_videoroom_hangup_media(handle);
1069
                session->destroyed = janus_get_monotonic_time();
1070
                old_sessions = g_list_append(old_sessions, session);
1071
                if(session->participant_type == janus_videoroom_p_type_publisher) {
1072
                        /* Get rid of publisher */
1073
                        janus_videoroom_participant *participant = (janus_videoroom_participant *)session->participant;
1074
                        participant->audio = FALSE;
1075
                        participant->video = FALSE;
1076
                        participant->data = FALSE;
1077
                        participant->audio_active = FALSE;
1078
                        participant->video_active = FALSE;
1079
                        participant->recording_active = FALSE;
1080
                        if(participant->recording_base)
1081
                                g_free(participant->recording_base);
1082
                        participant->recording_base = NULL;
1083
                        janus_videoroom_leave_or_unpublish(participant, TRUE);
1084
                } else if(session->participant_type == janus_videoroom_p_type_subscriber) {
1085
                        /* Detaching this listener from its publisher is already done by hangup_media */
1086
                } else if(session->participant_type == janus_videoroom_p_type_subscriber_muxed) {
1087
                        /* Detaching this listener from its publishers is already done by hangup_media */
1088
                }
1089
        }
1090
        janus_mutex_unlock(&sessions_mutex);
1091

    
1092
        return;
1093
}
1094

    
1095
char *janus_videoroom_query_session(janus_plugin_session *handle) {
1096
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized)) {
1097
                return NULL;
1098
        }        
1099
        janus_videoroom_session *session = (janus_videoroom_session *)handle->plugin_handle;
1100
        if(!session) {
1101
                JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
1102
                return NULL;
1103
        }
1104
        /* Show the participant/room info, if any */
1105
        json_t *info = json_object();
1106
        if(session->participant) {
1107
                if(session->participant_type == janus_videoroom_p_type_none) {
1108
                        json_object_set_new(info, "type", json_string("none"));
1109
                } else if(session->participant_type == janus_videoroom_p_type_publisher) {
1110
                        json_object_set_new(info, "type", json_string("publisher"));
1111
                        janus_videoroom_participant *participant = (janus_videoroom_participant *)session->participant;
1112
                        if(participant) {
1113
                                janus_videoroom *room = participant->room; 
1114
                                json_object_set_new(info, "room", room ? json_integer(room->room_id) : NULL);
1115
                                json_object_set_new(info, "id", json_integer(participant->user_id));
1116
                                if(participant->display)
1117
                                        json_object_set_new(info, "display", json_string(participant->display));
1118
                                if(participant->listeners)
1119
                                        json_object_set_new(info, "viewers", json_integer(g_slist_length(participant->listeners)));
1120
                                json_t *media = json_object();
1121
                                json_object_set_new(media, "audio", json_integer(participant->audio));
1122
                                json_object_set_new(media, "video", json_integer(participant->video));
1123
                                json_object_set_new(media, "data", json_integer(participant->data));
1124
                                json_object_set_new(info, "media", media);
1125
                                json_object_set_new(info, "bitrate", json_integer(participant->bitrate));
1126
                                if(participant->arc || participant->vrc) {
1127
                                        json_t *recording = json_object();
1128
                                        if(participant->arc && participant->arc->filename)
1129
                                                json_object_set_new(recording, "audio", json_string(participant->arc->filename));
1130
                                        if(participant->vrc && participant->vrc->filename)
1131
                                                json_object_set_new(recording, "video", json_string(participant->vrc->filename));
1132
                                        json_object_set_new(info, "recording", recording);
1133
                                }
1134
                        }
1135
                } else if(session->participant_type == janus_videoroom_p_type_subscriber) {
1136
                        json_object_set_new(info, "type", json_string("listener"));
1137
                        janus_videoroom_listener *participant = (janus_videoroom_listener *)session->participant;
1138
                        if(participant) {
1139
                                janus_videoroom_participant *feed = (janus_videoroom_participant *)participant->feed;
1140
                                if(feed) {
1141
                                        janus_videoroom *room = feed->room; 
1142
                                        json_object_set_new(info, "room", room ? json_integer(room->room_id) : NULL);
1143
                                        json_object_set_new(info, "feed_id", json_integer(feed->user_id));
1144
                                        if(feed->display)
1145
                                                json_object_set_new(info, "feed_display", json_string(feed->display));
1146
                                }
1147
                                json_t *media = json_object();
1148
                                json_object_set_new(media, "audio", json_integer(participant->audio));
1149
                                json_object_set_new(media, "video", json_integer(participant->video));
1150
                                json_object_set_new(media, "data", json_integer(participant->data));
1151
                                json_object_set_new(info, "media", media);
1152
                        }
1153
                } else if(session->participant_type == janus_videoroom_p_type_subscriber_muxed) {
1154
                        json_object_set_new(info, "type", json_string("muxed-listener"));
1155
                        /* TODO */
1156
                }
1157
        }
1158
        json_object_set_new(info, "destroyed", json_integer(session->destroyed));
1159
        char *info_text = json_dumps(info, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1160
        json_decref(info);
1161
        return info_text;
1162
}
1163

    
1164
static int janus_videoroom_access_room(json_t *root, gboolean check_secret, gboolean check_pin, janus_videoroom **videoroom, char *error_cause, int error_cause_size) {
1165
        /* rooms_mutex has to be locked */
1166
        int error_code = 0;
1167
        json_t *room = json_object_get(root, "room");
1168
        guint64 room_id = json_integer_value(room);
1169
        *videoroom = g_hash_table_lookup(rooms, &room_id);
1170
        if(*videoroom == NULL) {
1171
                JANUS_LOG(LOG_ERR, "No such room (%"SCNu64")\n", room_id);
1172
                error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_ROOM;
1173
                if(error_cause)
1174
                        g_snprintf(error_cause, error_cause_size, "No such room (%"SCNu64")", room_id);
1175
                return error_code;
1176
        }
1177
        if((*videoroom)->destroyed) {
1178
                JANUS_LOG(LOG_ERR, "No such room (%"SCNu64")\n", room_id);
1179
                error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_ROOM;
1180
                if(error_cause)
1181
                        g_snprintf(error_cause, error_cause_size, "No such room (%"SCNu64")", room_id);
1182
                return error_code;
1183
        }
1184
        if(check_secret) {
1185
                char error_cause2[100];
1186
                JANUS_CHECK_SECRET((*videoroom)->room_secret, root, "secret", error_code, error_cause2,
1187
                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT, JANUS_VIDEOROOM_ERROR_UNAUTHORIZED);
1188
                if(error_code != 0) {
1189
                        g_strlcpy(error_cause, error_cause2, error_cause_size);
1190
                        return error_code;
1191
                }
1192
        }
1193
        if(check_pin) {
1194
                char error_cause2[100];
1195
                JANUS_CHECK_SECRET((*videoroom)->room_pin, root, "pin", error_code, error_cause2,
1196
                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT, JANUS_VIDEOROOM_ERROR_UNAUTHORIZED);
1197
                if(error_code != 0) {
1198
                        g_strlcpy(error_cause, error_cause2, error_cause_size);
1199
                        return error_code;
1200
                }
1201
        }
1202
        return 0;
1203
}
1204

    
1205
struct janus_plugin_result *janus_videoroom_handle_message(janus_plugin_session *handle, char *transaction, char *message, char *sdp_type, char *sdp) {
1206
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
1207
                return janus_plugin_result_new(JANUS_PLUGIN_ERROR, g_atomic_int_get(&stopping) ? "Shutting down" : "Plugin not initialized");
1208
        
1209
        /* Pre-parse the message */
1210
        int error_code = 0;
1211
        char error_cause[512];
1212
        json_t *root = NULL;
1213
        json_t *response = NULL;
1214

    
1215
        if(message == NULL) {
1216
                JANUS_LOG(LOG_ERR, "No message??\n");
1217
                error_code = JANUS_VIDEOROOM_ERROR_NO_MESSAGE;
1218
                g_snprintf(error_cause, 512, "%s", "No message??");
1219
                goto error;
1220
        }
1221
        JANUS_LOG(LOG_VERB, "Handling message: %s\n", message);
1222

    
1223
        janus_videoroom_session *session = (janus_videoroom_session *)handle->plugin_handle;        
1224
        if(!session) {
1225
                JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
1226
                error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;
1227
                g_snprintf(error_cause, 512, "%s", "session associated with this handle...");
1228
                goto error;
1229
        }
1230
        if(session->destroyed) {
1231
                JANUS_LOG(LOG_ERR, "Session has already been marked as destroyed...\n");
1232
                error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;
1233
                g_snprintf(error_cause, 512, "%s", "Session has already been marked as destroyed...");
1234
                goto error;
1235
        }
1236
        json_error_t error;
1237
        root = json_loads(message, 0, &error);
1238
        if(!root) {
1239
                JANUS_LOG(LOG_ERR, "JSON error: on line %d: %s\n", error.line, error.text);
1240
                error_code = JANUS_VIDEOROOM_ERROR_INVALID_JSON;
1241
                g_snprintf(error_cause, 512, "JSON error: on line %d: %s", error.line, error.text);
1242
                goto error;
1243
        }
1244
        if(!json_is_object(root)) {
1245
                JANUS_LOG(LOG_ERR, "JSON error: not an object\n");
1246
                error_code = JANUS_VIDEOROOM_ERROR_INVALID_JSON;
1247
                g_snprintf(error_cause, 512, "JSON error: not an object");
1248
                goto error;
1249
        }
1250
        /* Get the request first */
1251
        JANUS_VALIDATE_JSON_OBJECT(root, request_parameters,
1252
                error_code, error_cause, TRUE,
1253
                JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
1254
        if(error_code != 0)
1255
                goto error;
1256
        json_t *request = json_object_get(root, "request");
1257
        /* Some requests ('create', 'destroy', 'exists', 'list') can be handled synchronously */
1258
        const char *request_text = json_string_value(request);
1259
        if(!strcasecmp(request_text, "create")) {
1260
                /* Create a new videoroom */
1261
                JANUS_LOG(LOG_VERB, "Creating a new videoroom\n");
1262
                JANUS_VALIDATE_JSON_OBJECT(root, create_parameters,
1263
                        error_code, error_cause, TRUE,
1264
                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
1265
                if(error_code != 0)
1266
                        goto error;
1267
                if(admin_key != NULL) {
1268
                        /* An admin key was specified: make sure it was provided, and that it's valid */
1269
                        JANUS_VALIDATE_JSON_OBJECT(root, adminkey_parameters,
1270
                                error_code, error_cause, TRUE,
1271
                                JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
1272
                        if(error_code != 0)
1273
                                goto error;
1274
                        JANUS_CHECK_SECRET(admin_key, root, "admin_key", error_code, error_cause,
1275
                                JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT, JANUS_VIDEOROOM_ERROR_UNAUTHORIZED);
1276
                        if(error_code != 0)
1277
                                goto error;
1278
                }
1279
                json_t *desc = json_object_get(root, "description");
1280
                json_t *is_private = json_object_get(root, "is_private");
1281
                json_t *secret = json_object_get(root, "secret");
1282
                json_t *pin = json_object_get(root, "pin");
1283
                json_t *bitrate = json_object_get(root, "bitrate");
1284
                json_t *fir_freq = json_object_get(root, "fir_freq");
1285
                json_t *publishers = json_object_get(root, "publishers");
1286
                json_t *audiocodec = json_object_get(root, "audiocodec");
1287
                if(audiocodec) {
1288
                        const char *audiocodec_value = json_string_value(audiocodec);
1289
                        if(!strcasecmp(audiocodec_value, "opus") && !strcasecmp(audiocodec_value, "isac32") && !strcasecmp(audiocodec_value, "isac16") && !strcasecmp(audiocodec_value, "pcmu") && !strcasecmp(audiocodec_value, "pcma")) {
1290
                                JANUS_LOG(LOG_ERR, "Invalid element (audiocodec can only be opus, isac32, isac16, pcmu, or pcma)\n");
1291
                                error_code = JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT;
1292
                                g_snprintf(error_cause, 512, "Invalid element (audiocodec can only be opus, isac32, isac16, pcmu, or pcma)");
1293
                                goto error;
1294
                        }
1295
                }
1296
                json_t *videocodec = json_object_get(root, "videocodec");
1297
                if(videocodec) {
1298
                        const char *videocodec_value = json_string_value(videocodec);
1299
                        if(!strcasecmp(videocodec_value, "vp8") && !strcasecmp(videocodec_value, "vp9") && !strcasecmp(videocodec_value, "h264")) {
1300
                                JANUS_LOG(LOG_ERR, "Invalid element (videocodec can only be vp8, vp9 or h264)\n");
1301
                                error_code = JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT;
1302
                                g_snprintf(error_cause, 512, "Invalid element (videocodec can only be vp8, vp9 or h264)");
1303
                                goto error;
1304
                        }
1305
                }
1306
                json_t *record = json_object_get(root, "record");
1307
                json_t *rec_dir = json_object_get(root, "rec_dir");
1308
                json_t *permanent = json_object_get(root, "permanent");
1309
                gboolean save = permanent ? json_is_true(permanent) : FALSE;
1310
                if(save && config == NULL) {
1311
                        JANUS_LOG(LOG_ERR, "No configuration file, can't create permanent room\n");
1312
                        error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;
1313
                        g_snprintf(error_cause, 512, "No configuration file, can't create permanent room");
1314
                        goto error;
1315
                }
1316
                guint64 room_id = 0;
1317
                json_t *room = json_object_get(root, "room");
1318
                if(room) {
1319
                        room_id = json_integer_value(room);
1320
                        if(room_id == 0) {
1321
                                JANUS_LOG(LOG_WARN, "Desired room ID is 0, which is not allowed... picking random ID instead\n");
1322
                        }
1323
                }
1324
                janus_mutex_lock(&rooms_mutex);
1325
                if(room_id > 0) {
1326
                        /* Let's make sure the room doesn't exist already */
1327
                        if(g_hash_table_lookup(rooms, &room_id) != NULL) {
1328
                                /* It does... */
1329
                                janus_mutex_unlock(&rooms_mutex);
1330
                                JANUS_LOG(LOG_ERR, "Room %"SCNu64" already exists!\n", room_id);
1331
                                error_code = JANUS_VIDEOROOM_ERROR_ROOM_EXISTS;
1332
                                g_snprintf(error_cause, 512, "Room %"SCNu64" already exists", room_id);
1333
                                goto error;
1334
                        }
1335
                }
1336
                /* Create the room */
1337
                janus_videoroom *videoroom = g_malloc0(sizeof(janus_videoroom));
1338
                if(videoroom == NULL) {
1339
                        janus_mutex_unlock(&rooms_mutex);
1340
                        JANUS_LOG(LOG_FATAL, "Memory error!\n");
1341
                        error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;
1342
                        g_snprintf(error_cause, 512, "Memory error");
1343
                        goto error;
1344
                }
1345
                /* Generate a random ID */
1346
                if(room_id == 0) {
1347
                        while(room_id == 0) {
1348
                                room_id = janus_random_uint64();
1349
                                if(g_hash_table_lookup(rooms, &room_id) != NULL) {
1350
                                        /* Room ID already taken, try another one */
1351
                                        room_id = 0;
1352
                                }
1353
                        }
1354
                }
1355
                videoroom->room_id = room_id;
1356
                char *description = NULL;
1357
                if(desc != NULL && strlen(json_string_value(desc)) > 0) {
1358
                        description = g_strdup(json_string_value(desc));
1359
                } else {
1360
                        char roomname[255];
1361
                        g_snprintf(roomname, 255, "Room %"SCNu64"", videoroom->room_id);
1362
                        description = g_strdup(roomname);
1363
                }
1364
                if(description == NULL) {
1365
                        janus_mutex_unlock(&rooms_mutex);
1366
                        JANUS_LOG(LOG_FATAL, "Memory error!\n");
1367
                        error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;
1368
                        g_snprintf(error_cause, 512, "Memory error");
1369
                        goto error;
1370
                }
1371
                videoroom->room_name = description;
1372
                videoroom->is_private = is_private ? json_is_true(is_private) : FALSE;
1373
                if(secret)
1374
                        videoroom->room_secret = g_strdup(json_string_value(secret));
1375
                if(pin)
1376
                        videoroom->room_pin = g_strdup(json_string_value(pin));
1377
                videoroom->max_publishers = 3;        /* FIXME How should we choose a default? */
1378
                if(publishers)
1379
                        videoroom->max_publishers = json_integer_value(publishers);
1380
                if(videoroom->max_publishers < 0)
1381
                        videoroom->max_publishers = 3;        /* FIXME How should we choose a default? */
1382
                videoroom->bitrate = 0;
1383
                if(bitrate)
1384
                        videoroom->bitrate = json_integer_value(bitrate);
1385
                if(videoroom->bitrate > 0 && videoroom->bitrate < 64000)
1386
                        videoroom->bitrate = 64000;        /* Don't go below 64k */
1387
                videoroom->fir_freq = 0;
1388
                if(fir_freq)
1389
                        videoroom->fir_freq = json_integer_value(fir_freq);
1390
                videoroom->acodec = JANUS_VIDEOROOM_OPUS;
1391
                if(audiocodec) {
1392
                        const char *audiocodec_value = json_string_value(audiocodec);
1393
                        if(!strcasecmp(audiocodec_value, "opus"))
1394
                                videoroom->acodec = JANUS_VIDEOROOM_OPUS;
1395
                        else if(!strcasecmp(audiocodec_value, "isac32"))
1396
                                videoroom->acodec = JANUS_VIDEOROOM_ISAC_32K;
1397
                        else if(!strcasecmp(audiocodec_value, "isac16"))
1398
                                videoroom->acodec = JANUS_VIDEOROOM_ISAC_16K;
1399
                        else if(!strcasecmp(audiocodec_value, "pcmu"))
1400
                                videoroom->acodec = JANUS_VIDEOROOM_PCMU;
1401
                        else if(!strcasecmp(audiocodec_value, "pcma"))
1402
                                videoroom->acodec = JANUS_VIDEOROOM_PCMA;
1403
                        else {
1404
                                JANUS_LOG(LOG_WARN, "Unsupported audio codec '%s', falling back to OPUS\n", audiocodec_value);
1405
                                videoroom->acodec = JANUS_VIDEOROOM_OPUS;
1406
                        }
1407
                }
1408
                videoroom->vcodec = JANUS_VIDEOROOM_VP8;
1409
                if(videocodec) {
1410
                        const char *videocodec_value = json_string_value(videocodec);
1411
                        if(!strcasecmp(videocodec_value, "vp8"))
1412
                                videoroom->vcodec = JANUS_VIDEOROOM_VP8;
1413
                        else if(!strcasecmp(videocodec_value, "vp9"))
1414
                                videoroom->vcodec = JANUS_VIDEOROOM_VP9;
1415
                        else if(!strcasecmp(videocodec_value, "h264"))
1416
                                videoroom->vcodec = JANUS_VIDEOROOM_H264;
1417
                        else {
1418
                                JANUS_LOG(LOG_WARN, "Unsupported video codec '%s', falling back to VP8\n", videocodec_value);
1419
                                videoroom->vcodec = JANUS_VIDEOROOM_VP8;
1420
                        }
1421
                }
1422
                if(record) {
1423
                        videoroom->record = json_is_true(record);
1424
                }
1425
                if(rec_dir) {
1426
                        videoroom->rec_dir = g_strdup(json_string_value(rec_dir));
1427
                }
1428
                videoroom->destroyed = 0;
1429
                janus_mutex_init(&videoroom->participants_mutex);
1430
                videoroom->participants = g_hash_table_new_full(g_int64_hash, g_int64_equal, (GDestroyNotify)g_free, NULL);
1431
                JANUS_LOG(LOG_VERB, "Created videoroom: %"SCNu64" (%s, %s, %s/%s codecs, secret: %s, pin: %s)\n",
1432
                        videoroom->room_id, videoroom->room_name,
1433
                        videoroom->is_private ? "private" : "public",
1434
                        janus_videoroom_audiocodec_name(videoroom->acodec),
1435
                        janus_videoroom_videocodec_name(videoroom->vcodec),
1436
                        videoroom->room_secret ? videoroom->room_secret : "no secret",
1437
                        videoroom->room_pin ? videoroom->room_pin : "no pin");
1438
                if(videoroom->record) {
1439
                        JANUS_LOG(LOG_VERB, "  -- Room is going to be recorded in %s\n", videoroom->rec_dir ? videoroom->rec_dir : "the current folder");
1440
                }
1441
                if(save) {
1442
                        /* This room is permanent: save to the configuration file too
1443
                         * FIXME: We should check if anything fails... */
1444
                        JANUS_LOG(LOG_VERB, "Saving room %"SCNu64" permanently in config file\n", videoroom->room_id);
1445
                        janus_mutex_lock(&config_mutex);
1446
                        char cat[BUFSIZ], value[BUFSIZ];
1447
                        /* The room ID is the category */
1448
                        g_snprintf(cat, BUFSIZ, "%"SCNu64, videoroom->room_id);
1449
                        janus_config_add_category(config, cat);
1450
                        /* Now for the values */
1451
                        janus_config_add_item(config, cat, "description", videoroom->room_name);
1452
                        if(videoroom->is_private)
1453
                                janus_config_add_item(config, cat, "is_private", "yes");
1454
                        g_snprintf(value, BUFSIZ, "%"SCNu64, videoroom->bitrate);
1455
                        janus_config_add_item(config, cat, "bitrate", value);
1456
                        g_snprintf(value, BUFSIZ, "%d", videoroom->max_publishers);
1457
                        janus_config_add_item(config, cat, "publishers", value);
1458
                        if(videoroom->fir_freq) {
1459
                                g_snprintf(value, BUFSIZ, "%"SCNu16, videoroom->fir_freq);
1460
                                janus_config_add_item(config, cat, "fir_freq", value);
1461
                        }
1462
                        janus_config_add_item(config, cat, "audiocodec", janus_videoroom_audiocodec_name(videoroom->acodec));
1463
                        janus_config_add_item(config, cat, "videocodec", janus_videoroom_videocodec_name(videoroom->vcodec));
1464
                        if(videoroom->room_secret)
1465
                                janus_config_add_item(config, cat, "secret", videoroom->room_secret);
1466
                        if(videoroom->room_pin)
1467
                                janus_config_add_item(config, cat, "pin", videoroom->room_pin);
1468
                        if(videoroom->record)
1469
                                janus_config_add_item(config, cat, "record", "yes");
1470
                        if(videoroom->rec_dir)
1471
                                janus_config_add_item(config, cat, "rec_dir", videoroom->rec_dir);
1472
                        /* Save modified configuration */
1473
                        janus_config_save(config, config_folder, JANUS_VIDEOROOM_PACKAGE);
1474
                        janus_mutex_unlock(&config_mutex);
1475
                }
1476
                /* Show updated rooms list */
1477
                GHashTableIter iter;
1478
                gpointer value;
1479
                g_hash_table_insert(rooms, janus_uint64_dup(videoroom->room_id), videoroom);
1480
                g_hash_table_iter_init(&iter, rooms);
1481
                while (g_hash_table_iter_next(&iter, NULL, &value)) {
1482
                        janus_videoroom *vr = value;
1483
                        JANUS_LOG(LOG_VERB, "  ::: [%"SCNu64"][%s] %"SCNu64", max %d publishers, FIR frequency of %d seconds\n", vr->room_id, vr->room_name, vr->bitrate, vr->max_publishers, vr->fir_freq);
1484
                }
1485
                janus_mutex_unlock(&rooms_mutex);
1486
                /* Send info back */
1487
                response = json_object();
1488
                json_object_set_new(response, "videoroom", json_string("created"));
1489
                json_object_set_new(response, "room", json_integer(videoroom->room_id));
1490
                /* Also notify event handlers */
1491
                if(notify_events && gateway->events_is_enabled()) {
1492
                        json_t *info = json_object();
1493
                        json_object_set_new(info, "event", json_string("created"));
1494
                        json_object_set_new(info, "room", json_integer(videoroom->room_id));
1495
                        gateway->notify_event(session->handle, info);
1496
                }
1497
                goto plugin_response;
1498
        } else if(!strcasecmp(request_text, "destroy")) {
1499
                JANUS_LOG(LOG_VERB, "Attempt to destroy an existing videoroom room\n");
1500
                JANUS_VALIDATE_JSON_OBJECT(root, destroy_parameters,
1501
                        error_code, error_cause, TRUE,
1502
                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
1503
                if(error_code != 0)
1504
                        goto error;
1505
                json_t *room = json_object_get(root, "room");
1506
                json_t *permanent = json_object_get(root, "permanent");
1507
                gboolean save = permanent ? json_is_true(permanent) : FALSE;
1508
                if(save && config == NULL) {
1509
                        JANUS_LOG(LOG_ERR, "No configuration file, can't destroy room permanently\n");
1510
                        error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;
1511
                        g_snprintf(error_cause, 512, "No configuration file, can't destroy room permanently");
1512
                        goto error;
1513
                }
1514
                guint64 room_id = json_integer_value(room);
1515
                janus_mutex_lock(&rooms_mutex);
1516
                janus_videoroom *videoroom = NULL;
1517
                error_code = janus_videoroom_access_room(root, TRUE, FALSE, &videoroom, error_cause, sizeof(error_cause));
1518
                if(error_code != 0) {
1519
                        janus_mutex_unlock(&rooms_mutex);
1520
                        goto error;
1521
                }
1522
                /* Notify all participants that the fun is over, and that they'll be kicked */
1523
                JANUS_LOG(LOG_VERB, "Notifying all participants\n");
1524
                json_t *destroyed = json_object();
1525
                json_object_set_new(destroyed, "videoroom", json_string("destroyed"));
1526
                json_object_set_new(destroyed, "room", json_integer(videoroom->room_id));
1527
                char *destroyed_text = json_dumps(destroyed, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1528
                GHashTableIter iter;
1529
                gpointer value;
1530
                /* Remove room lazily*/
1531
                videoroom->destroyed = janus_get_monotonic_time();
1532
                old_rooms = g_list_append(old_rooms, videoroom);
1533
                janus_mutex_lock(&videoroom->participants_mutex);
1534
                g_hash_table_iter_init(&iter, videoroom->participants);
1535
                while (g_hash_table_iter_next(&iter, NULL, &value)) {
1536
                        janus_videoroom_participant *p = value;
1537
                        if(p && p->session) {
1538
                                /* Notify the user we're going to destroy the room... */
1539
                                int ret = gateway->push_event(p->session->handle, &janus_videoroom_plugin, NULL, destroyed_text, NULL, NULL);
1540
                                JANUS_LOG(LOG_VERB, "  >> %d (%s)\n", ret, janus_get_api_error(ret));
1541
                                /* ... and then ask the core to remove the handle */
1542
                                gateway->end_session(p->session->handle);
1543
                        }
1544
                }
1545
                json_decref(destroyed);
1546
                g_free(destroyed_text);
1547
                janus_mutex_unlock(&videoroom->participants_mutex);
1548
                /* Also notify event handlers */
1549
                if(notify_events && gateway->events_is_enabled()) {
1550
                        json_t *info = json_object();
1551
                        json_object_set_new(info, "event", json_string("destroyed"));
1552
                        json_object_set_new(info, "room", json_integer(room_id));
1553
                        gateway->notify_event(session->handle, info);
1554
                }
1555
                janus_mutex_unlock(&rooms_mutex);
1556
                if(save) {
1557
                        /* This change is permanent: save to the configuration file too
1558
                         * FIXME: We should check if anything fails... */
1559
                        JANUS_LOG(LOG_VERB, "Destroying room %"SCNu64" permanently in config file\n", room_id);
1560
                        janus_mutex_lock(&config_mutex);
1561
                        char cat[BUFSIZ];
1562
                        /* The room ID is the category */
1563
                        g_snprintf(cat, BUFSIZ, "%"SCNu64, room_id);
1564
                        janus_config_remove_category(config, cat);
1565
                        /* Save modified configuration */
1566
                        janus_config_save(config, config_folder, JANUS_VIDEOROOM_PACKAGE);
1567
                        janus_mutex_unlock(&config_mutex);
1568
                }
1569
                /* Done */
1570
                response = json_object();
1571
                json_object_set_new(response, "videoroom", json_string("destroyed"));
1572
                json_object_set_new(response, "room", json_integer(room_id));
1573
                goto plugin_response;
1574
        } else if(!strcasecmp(request_text, "list")) {
1575
                /* List all rooms (but private ones) and their details (except for the secret, of course...) */
1576
                json_t *list = json_array();
1577
                JANUS_LOG(LOG_VERB, "Getting the list of video rooms\n");
1578
                janus_mutex_lock(&rooms_mutex);
1579
                GHashTableIter iter;
1580
                gpointer value;
1581
                g_hash_table_iter_init(&iter, rooms);
1582
                while(g_hash_table_iter_next(&iter, NULL, &value)) {
1583
                        janus_videoroom *room = value;
1584
                        if(!room)
1585
                                continue;
1586
                        if(room->is_private) {
1587
                                /* Skip private room */
1588
                                JANUS_LOG(LOG_VERB, "Skipping private room '%s'\n", room->room_name);
1589
                                continue;
1590
                        }
1591
                        if(!room->destroyed) {
1592
                                json_t *rl = json_object();
1593
                                json_object_set_new(rl, "room", json_integer(room->room_id));
1594
                                json_object_set_new(rl, "description", json_string(room->room_name));
1595
                                json_object_set_new(rl, "max_publishers", json_integer(room->max_publishers));
1596
                                json_object_set_new(rl, "bitrate", json_integer(room->bitrate));
1597
                                json_object_set_new(rl, "fir_freq", json_integer(room->fir_freq));
1598
                                json_object_set_new(rl, "audiocodec", json_string(janus_videoroom_audiocodec_name(room->acodec)));
1599
                                json_object_set_new(rl, "videocodec", json_string(janus_videoroom_videocodec_name(room->vcodec)));
1600
                                json_object_set_new(rl, "record", json_string(room->record ? "true" : "false"));
1601
                                json_object_set_new(rl, "rec_dir", json_string(room->rec_dir));
1602
                                /* TODO: Should we list participants as well? or should there be a separate API call on a specific room for this? */
1603
                                json_object_set_new(rl, "num_participants", json_integer(g_hash_table_size(room->participants)));
1604
                                json_array_append_new(list, rl);
1605
                        }
1606
                }
1607
                janus_mutex_unlock(&rooms_mutex);
1608
                response = json_object();
1609
                json_object_set_new(response, "videoroom", json_string("success"));
1610
                json_object_set_new(response, "list", list);
1611
                goto plugin_response;
1612
        } else if(!strcasecmp(request_text, "rtp_forward")) {
1613
                JANUS_VALIDATE_JSON_OBJECT(root, rtp_forward_parameters,
1614
                        error_code, error_cause, TRUE,
1615
                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
1616
                if(error_code != 0)
1617
                        goto error;
1618
                json_t *room = json_object_get(root, "room");
1619
                json_t *pub_id = json_object_get(root, "publisher_id");
1620
                int video_port = -1;
1621
                int audio_port = -1;
1622
                json_t *vid_port = json_object_get(root, "video_port");
1623
                if(vid_port) {
1624
                        video_port = json_integer_value(vid_port);
1625
                }
1626
                json_t *au_port = json_object_get(root, "audio_port");
1627
                if(au_port) {
1628
                        audio_port = json_integer_value(au_port);
1629
                }
1630
                json_t *json_host = json_object_get(root, "host");
1631
                
1632
                guint64 room_id = json_integer_value(room);
1633
                guint64 publisher_id = json_integer_value(pub_id);
1634
                const gchar* host = json_string_value(json_host);
1635
                janus_mutex_lock(&rooms_mutex);
1636
                janus_videoroom *videoroom = NULL;
1637
                error_code = janus_videoroom_access_room(root, TRUE, FALSE, &videoroom, error_cause, sizeof(error_cause));
1638
                janus_mutex_unlock(&rooms_mutex);
1639
                if(error_code != 0)
1640
                        goto error;
1641
                janus_mutex_lock(&videoroom->participants_mutex);
1642
                janus_videoroom_participant* publisher = g_hash_table_lookup(videoroom->participants, &publisher_id);
1643
                if(publisher == NULL) {
1644
                        janus_mutex_unlock(&videoroom->participants_mutex);
1645
                        JANUS_LOG(LOG_ERR, "No such publisher (%"SCNu64")\n", publisher_id);
1646
                        error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_FEED;
1647
                        g_snprintf(error_cause, 512, "No such feed (%"SCNu64")", publisher_id);
1648
                        goto error;
1649
                }
1650
                if(publisher->udp_sock <= 0) {
1651
                        publisher->udp_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1652
                        if(publisher->udp_sock <= 0) {
1653
                                janus_mutex_unlock(&videoroom->participants_mutex);
1654
                                JANUS_LOG(LOG_ERR, "Could not open UDP socket for rtp stream for publisher (%"SCNu64")\n", publisher_id);
1655
                                error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;
1656
                                g_snprintf(error_cause, 512, "Could not open UDP socket for rtp stream");
1657
                                goto error;
1658
                        }
1659
                }
1660
                guint32 audio_handle = 0;
1661
                guint32 video_handle = 0;
1662
                if(audio_port > 0) {
1663
                        audio_handle = janus_rtp_forwarder_add_helper(publisher, host, audio_port, 0);
1664
                }
1665
                if(video_port > 0) {
1666
                        video_handle = janus_rtp_forwarder_add_helper(publisher, host, video_port, 1);
1667
                }
1668
                janus_mutex_unlock(&videoroom->participants_mutex);
1669
                response = json_object();
1670
                json_t* rtp_stream = json_object();
1671
                if(audio_handle > 0) {
1672
                        json_object_set_new(rtp_stream, "audio_stream_id", json_integer(audio_handle));
1673
                        json_object_set_new(rtp_stream, "audio", json_integer(audio_port));
1674
                }
1675
                if(video_handle > 0) {
1676
                        /* Send a FIR to the new RTP forward publisher */
1677
                        char buf[20];
1678
                        memset(buf, 0, 20);
1679
                        janus_rtcp_fir((char *)&buf, 20, &publisher->fir_seq);
1680
                        JANUS_LOG(LOG_VERB, "New RTP forward publisher, sending FIR to %"SCNu64" (%s)\n", publisher->user_id, publisher->display ? publisher->display : "??");
1681
                        gateway->relay_rtcp(publisher->session->handle, 1, buf, 20);
1682
                        /* Send a PLI too, just in case... */
1683
                        memset(buf, 0, 12);
1684
                        janus_rtcp_pli((char *)&buf, 12);
1685
                        JANUS_LOG(LOG_VERB, "New RTP forward publisher, sending PLI to %"SCNu64" (%s)\n", publisher->user_id, publisher->display ? publisher->display : "??");
1686
                        gateway->relay_rtcp(publisher->session->handle, 1, buf, 12);
1687
                        /* Done */
1688
                        json_object_set_new(rtp_stream, "video_stream_id", json_integer(video_handle));
1689
                        json_object_set_new(rtp_stream, "video", json_integer(video_port));
1690
                }
1691
                json_object_set_new(rtp_stream, "host", json_string(host));
1692
                json_object_set_new(response, "publisher_id", json_integer(publisher_id));
1693
                json_object_set_new(response, "rtp_stream", rtp_stream);
1694
                json_object_set_new(response, "room", json_integer(room_id));
1695
                json_object_set_new(response, "videoroom", json_string("rtp_forward"));
1696
                goto plugin_response;
1697
        } else if(!strcasecmp(request_text, "stop_rtp_forward")) {
1698
                JANUS_VALIDATE_JSON_OBJECT(root, stop_rtp_forward_parameters,
1699
                        error_code, error_cause, TRUE,
1700
                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
1701
                if(error_code != 0)
1702
                        goto error;
1703
                json_t *room = json_object_get(root, "room");
1704
                json_t *pub_id = json_object_get(root, "publisher_id");
1705
                json_t *id = json_object_get(root, "stream_id");
1706

    
1707
                guint64 room_id = json_integer_value(room);
1708
                guint64 publisher_id = json_integer_value(pub_id);
1709
                guint32 stream_id = json_integer_value(id);
1710
                janus_mutex_lock(&rooms_mutex);
1711
                janus_videoroom *videoroom = NULL;
1712
                error_code = janus_videoroom_access_room(root, TRUE, FALSE, &videoroom, error_cause, sizeof(error_cause));
1713
                janus_mutex_unlock(&rooms_mutex);
1714
                if(error_code != 0)
1715
                        goto error;
1716
                janus_mutex_lock(&videoroom->participants_mutex);
1717
                janus_videoroom_participant *publisher = g_hash_table_lookup(videoroom->participants, &publisher_id);
1718
                if(publisher == NULL) {
1719
                        janus_mutex_unlock(&videoroom->participants_mutex);
1720
                        JANUS_LOG(LOG_ERR, "No such publisher (%"SCNu64")\n", publisher_id);
1721
                        error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_FEED;
1722
                        g_snprintf(error_cause, 512, "No such feed (%"SCNu64")", publisher_id);
1723
                        goto error;
1724
                }
1725
                janus_mutex_lock(&publisher->rtp_forwarders_mutex);
1726
                if(g_hash_table_lookup(publisher->rtp_forwarders, GUINT_TO_POINTER(stream_id)) == NULL) {
1727
                        janus_mutex_unlock(&publisher->rtp_forwarders_mutex);
1728
                        janus_mutex_unlock(&videoroom->participants_mutex);
1729
                        JANUS_LOG(LOG_ERR, "No such stream (%"SCNu32")\n", stream_id);
1730
                        error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_FEED;
1731
                        g_snprintf(error_cause, 512, "No such stream (%"SCNu32")", stream_id);
1732
                        goto error;
1733
                }
1734
                g_hash_table_remove(publisher->rtp_forwarders, GUINT_TO_POINTER(stream_id));
1735
                janus_mutex_unlock(&publisher->rtp_forwarders_mutex);
1736
                janus_mutex_unlock(&videoroom->participants_mutex);
1737
                response = json_object();
1738
                json_object_set_new(response, "videoroom", json_string("stop_rtp_forward"));
1739
                json_object_set_new(response, "room", json_integer(room_id));
1740
                json_object_set_new(response, "publisher_id", json_integer(publisher_id));
1741
                json_object_set_new(response, "stream_id", json_integer(stream_id));
1742
                goto plugin_response;
1743
        } else if(!strcasecmp(request_text, "exists")) {
1744
                /* Check whether a given room exists or not, returns true/false */        
1745
                JANUS_VALIDATE_JSON_OBJECT(root, room_parameters,
1746
                        error_code, error_cause, TRUE,
1747
                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
1748
                if(error_code != 0)
1749
                        goto error;
1750
                json_t *room = json_object_get(root, "room");
1751
                guint64 room_id = json_integer_value(room);
1752
                janus_mutex_lock(&rooms_mutex);
1753
                gboolean room_exists = g_hash_table_contains(rooms, &room_id);
1754
                janus_mutex_unlock(&rooms_mutex);
1755
                response = json_object();
1756
                json_object_set_new(response, "videoroom", json_string("success"));
1757
                json_object_set_new(response, "room", json_integer(room_id));
1758
                json_object_set_new(response, "exists", json_string(room_exists ? "true" : "false"));
1759
                goto plugin_response;
1760
        } else if(!strcasecmp(request_text, "listparticipants")) {
1761
                /* List all participants in a room, specifying whether they're publishers or just attendees */        
1762
                JANUS_VALIDATE_JSON_OBJECT(root, room_parameters,
1763
                        error_code, error_cause, TRUE,
1764
                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
1765
                if(error_code != 0)
1766
                        goto error;
1767
                json_t *room = json_object_get(root, "room");
1768
                guint64 room_id = json_integer_value(room);
1769
                janus_mutex_lock(&rooms_mutex);
1770
                janus_videoroom *videoroom = NULL;
1771
                error_code = janus_videoroom_access_room(root, FALSE, FALSE, &videoroom, error_cause, sizeof(error_cause));
1772
                janus_mutex_unlock(&rooms_mutex);
1773
                if(error_code != 0)
1774
                        goto error;
1775
                /* Return a list of all participants (whether they're publishing or not) */
1776
                json_t *list = json_array();
1777
                GHashTableIter iter;
1778
                gpointer value;
1779
                janus_mutex_lock(&videoroom->participants_mutex);
1780
                g_hash_table_iter_init(&iter, videoroom->participants);
1781
                while (!videoroom->destroyed && g_hash_table_iter_next(&iter, NULL, &value)) {
1782
                        janus_videoroom_participant *p = value;
1783
                        json_t *pl = json_object();
1784
                        json_object_set_new(pl, "id", json_integer(p->user_id));
1785
                        if(p->display)
1786
                                json_object_set_new(pl, "display", json_string(p->display));
1787
                        json_object_set_new(pl, "publisher", json_string(p->sdp ? "true" : "false"));
1788
                        json_array_append_new(list, pl);
1789
                }
1790
                janus_mutex_unlock(&videoroom->participants_mutex);
1791
                response = json_object();
1792
                json_object_set_new(response, "videoroom", json_string("participants"));
1793
                json_object_set_new(response, "room", json_integer(room_id));
1794
                json_object_set_new(response, "participants", list);
1795
                goto plugin_response;
1796
        } else if(!strcasecmp(request_text, "listforwarders")) {
1797
                /* List all forwarders in a room */        
1798
                JANUS_VALIDATE_JSON_OBJECT(root, room_parameters,
1799
                        error_code, error_cause, TRUE,
1800
                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
1801
                if(error_code != 0)
1802
                        goto error;
1803
                json_t *room = json_object_get(root, "room");
1804
                guint64 room_id = json_integer_value(room);
1805
                janus_mutex_lock(&rooms_mutex);
1806
                janus_videoroom *videoroom = g_hash_table_lookup(rooms, &room_id);
1807
                if(videoroom == NULL) {
1808
                        JANUS_LOG(LOG_ERR, "No such room (%"SCNu64")\n", room_id);
1809
                        error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_ROOM;
1810
                        g_snprintf(error_cause, 512, "No such room (%"SCNu64")", room_id);
1811
                        janus_mutex_unlock(&rooms_mutex);
1812
                        goto error;
1813
                }
1814
                if(videoroom->destroyed) {
1815
                        JANUS_LOG(LOG_ERR, "No such room (%"SCNu64")\n", room_id);
1816
                        error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_ROOM;
1817
                        g_snprintf(error_cause, 512, "No such room (%"SCNu64")", room_id);
1818
                        janus_mutex_unlock(&rooms_mutex);
1819
                        goto error;
1820
                }
1821
                /* A secret may be required for this action */
1822
                JANUS_CHECK_SECRET(videoroom->room_secret, root, "secret", error_code, error_cause,
1823
                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT, JANUS_VIDEOROOM_ERROR_UNAUTHORIZED);
1824
                if(error_code != 0) {
1825
                        janus_mutex_unlock(&rooms_mutex);
1826
                        goto error;
1827
                }
1828
                /* Return a list of all forwarders */
1829
                json_t *list = json_array();
1830
                GHashTableIter iter;
1831
                gpointer value;
1832
                janus_mutex_lock(&videoroom->participants_mutex);
1833
                g_hash_table_iter_init(&iter, videoroom->participants);
1834
                while (!videoroom->destroyed && g_hash_table_iter_next(&iter, NULL, &value)) {
1835
                        janus_videoroom_participant *p = value;
1836
                        if(g_hash_table_size(p->rtp_forwarders) == 0)
1837
                                continue;
1838
                        json_t *pl = json_object();
1839
                        json_object_set_new(pl, "publisher_id", json_integer(p->user_id));
1840
                        if(p->display)
1841
                                json_object_set_new(pl, "display", json_string(p->display));
1842
                        json_t *flist = json_array();
1843
                        GHashTableIter iter_f;
1844
                        gpointer key_f, value_f;                        
1845
                        g_hash_table_iter_init(&iter_f, p->rtp_forwarders);
1846
                        janus_mutex_lock(&p->rtp_forwarders_mutex);
1847
                        while(g_hash_table_iter_next(&iter_f, &key_f, &value_f)) {                                
1848
                                json_t *fl = json_object();
1849
                                guint32 rpk = GPOINTER_TO_UINT(key_f);
1850
                                rtp_forwarder *rpv = value_f;
1851
                                json_object_set_new(fl, "ip" , json_string(inet_ntoa(rpv->serv_addr.sin_addr)));
1852
                                if(rpv->is_video > 0) {
1853
                                        json_object_set_new(fl, "video_stream_id" , json_integer(rpk));
1854
                                        json_object_set_new(fl, "port" , json_integer(ntohs(rpv->serv_addr.sin_port)));
1855
                                } else {
1856
                                        json_object_set_new(fl, "audio_stream_id" , json_integer(rpk));
1857
                                        json_object_set_new(fl, "port" , json_integer(ntohs(rpv->serv_addr.sin_port)));
1858
                                }
1859
                        json_array_append_new(flist, fl);
1860
                        }                
1861
                        janus_mutex_unlock(&p->rtp_forwarders_mutex);
1862
                        json_object_set_new(pl, "rtp_forwarder", flist);
1863
                        json_array_append_new(list, pl);
1864
                }
1865
                janus_mutex_unlock(&videoroom->participants_mutex);
1866
                janus_mutex_unlock(&rooms_mutex);
1867
                response = json_object();
1868
                json_object_set_new(response, "room", json_integer(room_id));
1869
                json_object_set_new(response, "rtp_forwarders", list);
1870
                goto plugin_response;
1871
        } else if(!strcasecmp(request_text, "join") || !strcasecmp(request_text, "joinandconfigure")
1872
                        || !strcasecmp(request_text, "configure") || !strcasecmp(request_text, "publish") || !strcasecmp(request_text, "unpublish")
1873
                        || !strcasecmp(request_text, "start") || !strcasecmp(request_text, "pause") || !strcasecmp(request_text, "switch") || !strcasecmp(request_text, "stop")
1874
                        || !strcasecmp(request_text, "add") || !strcasecmp(request_text, "remove") || !strcasecmp(request_text, "leave")) {
1875
                /* These messages are handled asynchronously */
1876

    
1877
                janus_videoroom_message *msg = g_malloc0(sizeof(janus_videoroom_message));
1878
                if(msg == NULL) {
1879
                        JANUS_LOG(LOG_FATAL, "Memory error!\n");
1880
                        error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;
1881
                        g_snprintf(error_cause, 512, "Memory error");
1882
                        goto error;
1883
                }
1884

    
1885
                g_free(message);
1886
                msg->handle = handle;
1887
                msg->transaction = transaction;
1888
                msg->message = root;
1889
                msg->sdp_type = sdp_type;
1890
                msg->sdp = sdp;
1891
                g_async_queue_push(messages, msg);
1892

    
1893
                return janus_plugin_result_new(JANUS_PLUGIN_OK_WAIT, NULL);
1894
        } else {
1895
                JANUS_LOG(LOG_VERB, "Unknown request '%s'\n", request_text);
1896
                error_code = JANUS_VIDEOROOM_ERROR_INVALID_REQUEST;
1897
                g_snprintf(error_cause, 512, "Unknown request '%s'", request_text);
1898
                goto error;
1899
        }
1900

    
1901
plugin_response:
1902
                {
1903
                        if(!response) {
1904
                                error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;
1905
                                g_snprintf(error_cause, 512, "Invalid response");
1906
                                goto error;
1907
                        }
1908
                        if(root != NULL)
1909
                                json_decref(root);
1910
                        g_free(transaction);
1911
                        g_free(message);
1912
                        g_free(sdp_type);
1913
                        g_free(sdp);
1914

    
1915
                        char *response_text = json_dumps(response, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1916
                        json_decref(response);
1917
                        janus_plugin_result *result = janus_plugin_result_new(JANUS_PLUGIN_OK, response_text);
1918
                        g_free(response_text);
1919
                        return result;
1920
                }
1921

    
1922
error:
1923
                {
1924
                        if(root != NULL)
1925
                                json_decref(root);
1926
                        g_free(transaction);
1927
                        g_free(message);
1928
                        g_free(sdp_type);
1929
                        g_free(sdp);
1930

    
1931
                        /* Prepare JSON error event */
1932
                        json_t *event = json_object();
1933
                        json_object_set_new(event, "videoroom", json_string("event"));
1934
                        json_object_set_new(event, "error_code", json_integer(error_code));
1935
                        json_object_set_new(event, "error", json_string(error_cause));
1936
                        char *event_text = json_dumps(event, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1937
                        json_decref(event);
1938
                        janus_plugin_result *result = janus_plugin_result_new(JANUS_PLUGIN_OK, event_text);
1939
                        g_free(event_text);
1940
                        return result;
1941
                }
1942

    
1943
}
1944

    
1945
void janus_videoroom_setup_media(janus_plugin_session *handle) {
1946
        JANUS_LOG(LOG_INFO, "WebRTC media is now available\n");
1947
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
1948
                return;
1949
        janus_videoroom_session *session = (janus_videoroom_session *)handle->plugin_handle;        
1950
        if(!session) {
1951
                JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
1952
                return;
1953
        }
1954
        if(session->destroyed)
1955
                return;
1956
        g_atomic_int_set(&session->hangingup, 0);
1957

    
1958
        /* Media relaying can start now */
1959
        session->started = TRUE;
1960

    
1961
        if(session->participant) {
1962
                /* If this is a publisher, notify all listeners about the fact they can
1963
                 * now subscribe; if this is a listener, instead, ask the publisher a FIR */
1964
                if(session->participant_type == janus_videoroom_p_type_publisher) {
1965
                        janus_videoroom_participant *participant = (janus_videoroom_participant *)session->participant;
1966
                        /* Notify all other participants that there's a new boy in town */
1967
                        json_t *list = json_array();
1968
                        json_t *pl = json_object();
1969
                        json_object_set_new(pl, "id", json_integer(participant->user_id));
1970
                        if(participant->display)
1971
                                json_object_set_new(pl, "display", json_string(participant->display));
1972
                        json_array_append_new(list, pl);
1973
                        json_t *pub = json_object();
1974
                        json_object_set_new(pub, "videoroom", json_string("event"));
1975
                        json_object_set_new(pub, "room", json_integer(participant->room->room_id));
1976
                        json_object_set_new(pub, "publishers", list);
1977
                        char *pub_text = json_dumps(pub, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1978
                        json_decref(pub);
1979
                        GHashTableIter iter;
1980
                        gpointer value;
1981
                        janus_videoroom *videoroom = participant->room;
1982
                        janus_mutex_lock(&videoroom->participants_mutex);
1983
                        g_hash_table_iter_init(&iter, videoroom->participants);
1984
                        while (!videoroom->destroyed && g_hash_table_iter_next(&iter, NULL, &value)) {
1985
                                janus_videoroom_participant *p = value;
1986
                                if(p == participant) {
1987
                                        continue;        /* Skip the new publisher itself */
1988
                                }
1989
                                JANUS_LOG(LOG_VERB, "Notifying participant %"SCNu64" (%s)\n", p->user_id, p->display ? p->display : "??");
1990
                                int ret = gateway->push_event(p->session->handle, &janus_videoroom_plugin, NULL, pub_text, NULL, NULL);
1991
                                JANUS_LOG(LOG_VERB, "  >> %d (%s)\n", ret, janus_get_api_error(ret));
1992
                        }
1993
                        g_free(pub_text);
1994
                        janus_mutex_unlock(&videoroom->participants_mutex);
1995
                        /* Also notify event handlers */
1996
                        if(notify_events && gateway->events_is_enabled()) {
1997
                                json_t *info = json_object();
1998
                                json_object_set_new(info, "event", json_string("published"));
1999
                                json_object_set_new(info, "room", json_integer(participant->room->room_id));
2000
                                json_object_set_new(info, "id", json_integer(participant->user_id));
2001
                                gateway->notify_event(session->handle, info);
2002
                        }
2003
                } else if(session->participant_type == janus_videoroom_p_type_subscriber) {
2004
                        janus_videoroom_listener *l = (janus_videoroom_listener *)session->participant;
2005
                        if(l && l->feed) {
2006
                                janus_videoroom_participant *p = l->feed;
2007
                                if(p && p->session) {
2008
                                        /* Send a FIR */
2009
                                        char buf[20];
2010
                                        memset(buf, 0, 20);
2011
                                        janus_rtcp_fir((char *)&buf, 20, &p->fir_seq);
2012
                                        JANUS_LOG(LOG_VERB, "New listener available, sending FIR to %"SCNu64" (%s)\n", p->user_id, p->display ? p->display : "??");
2013
                                        gateway->relay_rtcp(p->session->handle, 1, buf, 20);
2014
                                        /* Send a PLI too, just in case... */
2015
                                        memset(buf, 0, 12);
2016
                                        janus_rtcp_pli((char *)&buf, 12);
2017
                                        JANUS_LOG(LOG_VERB, "New listener available, sending PLI to %"SCNu64" (%s)\n", p->user_id, p->display ? p->display : "??");
2018
                                        gateway->relay_rtcp(p->session->handle, 1, buf, 12);
2019
                                        /* Also notify event handlers */
2020
                                        if(notify_events && gateway->events_is_enabled()) {
2021
                                                json_t *info = json_object();
2022
                                                json_object_set_new(info, "event", json_string("subscribed"));
2023
                                                json_object_set_new(info, "room", json_integer(p->room->room_id));
2024
                                                json_object_set_new(info, "feed", json_integer(p->user_id));
2025
                                                gateway->notify_event(session->handle, info);
2026
                                        }
2027
                                }
2028
                        }
2029
                } else if(session->participant_type == janus_videoroom_p_type_subscriber_muxed) {
2030
                        /* Do the same, but for all feeds */
2031
                        janus_videoroom_listener_muxed *listener = (janus_videoroom_listener_muxed *)session->participant;
2032
                        if(listener == NULL)
2033
                                return;
2034
                        GSList *ps = listener->listeners;
2035
                        while(ps) {
2036
                                janus_videoroom_listener *l = (janus_videoroom_listener *)ps->data;
2037
                                if(l && l->feed) {
2038
                                        janus_videoroom_participant *p = l->feed;
2039
                                        if(p && p->session) {
2040
                                                /* Send a FIR */
2041
                                                char buf[20];
2042
                                                memset(buf, 0, 20);
2043
                                                janus_rtcp_fir((char *)&buf, 20, &p->fir_seq);
2044
                                                JANUS_LOG(LOG_VERB, "New Multiplexed listener available, sending FIR to %"SCNu64" (%s)\n", p->user_id, p->display ? p->display : "??");
2045
                                                gateway->relay_rtcp(p->session->handle, 1, buf, 20);
2046
                                                /* Send a PLI too, just in case... */
2047
                                                memset(buf, 0, 12);
2048
                                                janus_rtcp_pli((char *)&buf, 12);
2049
                                                JANUS_LOG(LOG_VERB, "New Multiplexed listener available, sending PLI to %"SCNu64" (%s)\n", p->user_id, p->display ? p->display : "??");
2050
                                                gateway->relay_rtcp(p->session->handle, 1, buf, 12);
2051
                                        }
2052
                                }
2053
                                ps = ps->next;
2054
                        }
2055
                }
2056
        }
2057
}
2058

    
2059
void janus_videoroom_incoming_rtp(janus_plugin_session *handle, int video, char *buf, int len) {
2060
        if(handle == NULL || handle->stopped || g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized) || !gateway)
2061
                return;
2062
        janus_videoroom_session *session = (janus_videoroom_session *)handle->plugin_handle;
2063
        if(!session || session->destroyed || !session->participant || session->participant_type != janus_videoroom_p_type_publisher)
2064
                return;
2065
        janus_videoroom_participant *participant = (janus_videoroom_participant *)session->participant;
2066
        if((!video && participant->audio_active) || (video && participant->video_active)) {
2067
                /* Update payload type and SSRC */
2068
                rtp_header *rtp = (rtp_header *)buf;
2069
                rtp->type = video ? participant->video_pt : participant->audio_pt;
2070
                rtp->ssrc = htonl(video ? participant->video_ssrc : participant->audio_ssrc);
2071
                /* Forward RTP to the appropriate port for the rtp_forwarders associated wih this publisher, if there are any */
2072
                GHashTableIter iter;
2073
                gpointer value;
2074
                g_hash_table_iter_init(&iter, participant->rtp_forwarders);
2075
                janus_mutex_lock(&participant->rtp_forwarders_mutex);
2076
                while(participant->udp_sock > 0 && g_hash_table_iter_next(&iter, NULL, &value)) {
2077
                        rtp_forwarder* rtp_forward = (rtp_forwarder*)value;
2078
                        if(video && rtp_forward->is_video) {
2079
                                sendto(participant->udp_sock, buf, len, 0, (struct sockaddr*)&rtp_forward->serv_addr, sizeof(rtp_forward->serv_addr));
2080
                        }
2081
                        else if(!video && !rtp_forward->is_video) {
2082
                                sendto(participant->udp_sock, buf, len, 0, (struct sockaddr*)&rtp_forward->serv_addr, sizeof(rtp_forward->serv_addr));
2083
                        }
2084
                }
2085
                janus_mutex_unlock(&participant->rtp_forwarders_mutex);
2086
                /* Save the frame if we're recording */
2087
                janus_recorder_save_frame(video ? participant->vrc : participant->arc, buf, len);
2088
                /* Done, relay it */
2089
                janus_videoroom_rtp_relay_packet packet;
2090
                packet.data = rtp;
2091
                packet.length = len;
2092
                packet.is_video = video;
2093
                /* Backup the actual timestamp and sequence number set by the publisher, in case switching is involved */
2094
                packet.timestamp = ntohl(packet.data->timestamp);
2095
                packet.seq_number = ntohs(packet.data->seq_number);
2096
                /* Go */
2097
                g_slist_foreach(participant->listeners, janus_videoroom_relay_rtp_packet, &packet);
2098
                
2099
                /* Check if we need to send any REMB, FIR or PLI back to this publisher */
2100
                if(video && participant->video_active) {
2101
                        /* Did we send a REMB already, or is it time to send one? */
2102
                        gboolean send_remb = FALSE;
2103
                        if(participant->remb_latest == 0 && participant->remb_startup > 0) {
2104
                                /* Still in the starting phase, send the ramp-up REMB feedback */
2105
                                send_remb = TRUE;
2106
                        } else if(participant->remb_latest > 0 && janus_get_monotonic_time()-participant->remb_latest >= 5*G_USEC_PER_SEC) {
2107
                                /* 5 seconds have passed since the last REMB, send a new one */
2108
                                send_remb = TRUE;
2109
                        }                
2110
                        if(send_remb) {
2111
                                /* We send a few incremental REMB messages at startup */
2112
                                uint64_t bitrate = (participant->bitrate ? participant->bitrate : 256*1024);
2113
                                if(participant->remb_startup > 0) {
2114
                                        bitrate = bitrate/participant->remb_startup;
2115
                                        participant->remb_startup--;
2116
                                }
2117
                                JANUS_LOG(LOG_VERB, "Sending REMB (%s, %"SCNu64")\n", participant->display, bitrate);
2118
                                char rtcpbuf[24];
2119
                                janus_rtcp_remb((char *)(&rtcpbuf), 24, bitrate);
2120
                                gateway->relay_rtcp(handle, video, rtcpbuf, 24);
2121
                                if(participant->remb_startup == 0)
2122
                                        participant->remb_latest = janus_get_monotonic_time();
2123
                        }
2124
                        /* Generate FIR/PLI too, if needed */
2125
                        if(video && participant->video_active && (participant->room->fir_freq > 0)) {
2126
                                /* FIXME Very ugly hack to generate RTCP every tot seconds/frames */
2127
                                gint64 now = janus_get_monotonic_time();
2128
                                if((now-participant->fir_latest) >= (participant->room->fir_freq*G_USEC_PER_SEC)) {
2129
                                        /* FIXME We send a FIR every tot seconds */
2130
                                        participant->fir_latest = now;
2131
                                        char rtcpbuf[24];
2132
                                        memset(rtcpbuf, 0, 24);
2133
                                        janus_rtcp_fir((char *)&rtcpbuf, 20, &participant->fir_seq);
2134
                                        JANUS_LOG(LOG_VERB, "Sending FIR to %"SCNu64" (%s)\n", participant->user_id, participant->display ? participant->display : "??");
2135
                                        gateway->relay_rtcp(handle, video, rtcpbuf, 20);
2136
                                        /* Send a PLI too, just in case... */
2137
                                        memset(rtcpbuf, 0, 12);
2138
                                        janus_rtcp_pli((char *)&rtcpbuf, 12);
2139
                                        JANUS_LOG(LOG_VERB, "Sending PLI to %"SCNu64" (%s)\n", participant->user_id, participant->display ? participant->display : "??");
2140
                                        gateway->relay_rtcp(handle, video, rtcpbuf, 12);
2141
                                }
2142
                        }
2143
                }
2144
        }
2145
}
2146

    
2147
void janus_videoroom_incoming_rtcp(janus_plugin_session *handle, int video, char *buf, int len) {
2148
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
2149
                return;
2150
        janus_videoroom_session *session = (janus_videoroom_session *)handle->plugin_handle;        
2151
        if(!session) {
2152
                JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
2153
                return;
2154
        }
2155
        if(session->destroyed)
2156
                return;
2157
        if(session->participant_type == janus_videoroom_p_type_subscriber) {
2158
                /* A listener sent some RTCP, check what it is and if we need to forward it to the publisher */
2159
                janus_videoroom_listener *l = (janus_videoroom_listener *)session->participant;
2160
                if(!l->video)
2161
                        return;        /* The only feedback we handle is video related anyway... */
2162
                if(janus_rtcp_has_fir(buf, len)) {
2163
                        /* We got a FIR, forward it to the publisher */
2164
                        if(l && l->feed) {
2165
                                janus_videoroom_participant *p = l->feed;
2166
                                if(p && p->session) {
2167
                                        char rtcpbuf[20];
2168
                                        memset(rtcpbuf, 0, 20);
2169
                                        janus_rtcp_fir((char *)&rtcpbuf, 20, &p->fir_seq);
2170
                                        JANUS_LOG(LOG_VERB, "Got a FIR from a listener, forwarding it to %"SCNu64" (%s)\n", p->user_id, p->display ? p->display : "??");
2171
                                        gateway->relay_rtcp(p->session->handle, 1, rtcpbuf, 20);
2172
                                }
2173
                        }
2174
                }
2175
                if(janus_rtcp_has_pli(buf, len)) {
2176
                        /* We got a PLI, forward it to the publisher */
2177
                        if(l && l->feed) {
2178
                                janus_videoroom_participant *p = l->feed;
2179
                                if(p && p->session) {
2180
                                        char rtcpbuf[12];
2181
                                        memset(rtcpbuf, 0, 12);
2182
                                        janus_rtcp_pli((char *)&rtcpbuf, 12);
2183
                                        JANUS_LOG(LOG_VERB, "Got a PLI from a listener, forwarding it to %"SCNu64" (%s)\n", p->user_id, p->display ? p->display : "??");
2184
                                        gateway->relay_rtcp(p->session->handle, 1, rtcpbuf, 12);
2185
                                }
2186
                        }
2187
                }
2188
                uint64_t bitrate = janus_rtcp_get_remb(buf, len);
2189
                if(bitrate > 0) {
2190
                        /* FIXME We got a REMB from this listener, should we do something about it? */
2191
                }
2192
        }
2193
}
2194

    
2195
void janus_videoroom_incoming_data(janus_plugin_session *handle, char *buf, int len) {
2196
        if(handle == NULL || handle->stopped || g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized) || !gateway)
2197
                return;
2198
        if(buf == NULL || len <= 0)
2199
                return;
2200
        janus_videoroom_session *session = (janus_videoroom_session *)handle->plugin_handle;
2201
        if(!session || session->destroyed || !session->participant || session->participant_type != janus_videoroom_p_type_publisher)
2202
                return;
2203
        janus_videoroom_participant *participant = (janus_videoroom_participant *)session->participant;
2204
        /* Get a string out of the data */
2205
        char *text = g_malloc0(len+1);
2206
        memcpy(text, buf, len);
2207
        *(text+len) = '\0';
2208
        JANUS_LOG(LOG_VERB, "Got a DataChannel message (%zu bytes) to forward: %s\n", strlen(text), text);
2209
        g_slist_foreach(participant->listeners, janus_videoroom_relay_data_packet, text);
2210
        g_free(text);
2211
}
2212

    
2213
void janus_videoroom_slow_link(janus_plugin_session *handle, int uplink, int video) {
2214
        /* The core is informing us that our peer got too many NACKs, are we pushing media too hard? */
2215
        if(handle == NULL || handle->stopped || g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized) || !gateway)
2216
                return;
2217
        janus_videoroom_session *session = (janus_videoroom_session *)handle->plugin_handle;
2218
        if(!session || session->destroyed || !session->participant)
2219
                return;
2220
        /* Check if it's an uplink (publisher) or downlink (viewer) issue */
2221
        if(session->participant_type == janus_videoroom_p_type_publisher) {
2222
                if(!uplink) {
2223
                        janus_videoroom_participant *publisher = (janus_videoroom_participant *)session->participant;
2224
                        if(publisher) {
2225
                                /* Send an event on the handle to notify the application: it's
2226
                                 * up to the application to then choose a policy and enforce it */
2227
                                json_t *event = json_object();
2228
                                json_object_set_new(event, "videoroom", json_string("slow_link"));
2229
                                /* Also add info on what the current bitrate cap is */
2230
                                uint64_t bitrate = (publisher->bitrate ? publisher->bitrate : 256*1024);
2231
                                json_object_set_new(event, "current-bitrate", json_integer(bitrate));
2232
                                char *event_text = json_dumps(event, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
2233
                                json_decref(event);
2234
                                event = NULL;
2235
                                gateway->push_event(session->handle, &janus_videoroom_plugin, NULL, event_text, NULL, NULL);
2236
                                g_free(event_text);
2237
                                event_text = NULL;
2238
                        }
2239
                } else {
2240
                        JANUS_LOG(LOG_WARN, "Got a slow uplink on a VideoRoom publisher? Weird, because it doesn't receive media...\n");
2241
                }
2242
        } else if(session->participant_type == janus_videoroom_p_type_subscriber) {
2243
                if(uplink) {
2244
                        janus_videoroom_listener *viewer = (janus_videoroom_listener *)session->participant;
2245
                        if(viewer) {
2246
                                /* Send an event on the handle to notify the application: it's
2247
                                 * up to the application to then choose a policy and enforce it */
2248
                                json_t *event = json_object();
2249
                                json_object_set_new(event, "videoroom", json_string("slow_link"));
2250
                                char *event_text = json_dumps(event, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
2251
                                json_decref(event);
2252
                                event = NULL;
2253
                                gateway->push_event(session->handle, &janus_videoroom_plugin, NULL, event_text, NULL, NULL);
2254
                                g_free(event_text);
2255
                                event_text = NULL;
2256
                        }
2257
                } else {
2258
                        JANUS_LOG(LOG_WARN, "Got a slow downlink on a VideoRoom viewer? Weird, because it doesn't send media...\n");
2259
                }
2260
        } else if(session->participant_type == janus_videoroom_p_type_subscriber_muxed) {
2261
                /* TBD. */
2262
        }
2263
}
2264

    
2265
static void janus_videoroom_recorder_create(janus_videoroom_participant *participant, gboolean audio, gboolean video) {
2266
        char filename[255];
2267
        gint64 now = janus_get_real_time();
2268
        if(audio) {
2269
                memset(filename, 0, 255);
2270
                if(participant->recording_base) {
2271
                        /* Use the filename and path we have been provided */
2272
                        g_snprintf(filename, 255, "%s-audio", participant->recording_base);
2273
                        participant->arc = janus_recorder_create(participant->room->rec_dir,
2274
                                janus_videoroom_audiocodec_name(participant->room->acodec), filename);
2275
                        if(participant->arc == NULL) {
2276
                                JANUS_LOG(LOG_ERR, "Couldn't open an audio recording file for this publisher!\n");
2277
                        }
2278
                } else {
2279
                        /* Build a filename */
2280
                        g_snprintf(filename, 255, "videoroom-%"SCNu64"-user-%"SCNu64"-%"SCNi64"-audio",
2281
                                participant->room->room_id, participant->user_id, now);
2282
                        participant->arc = janus_recorder_create(participant->room->rec_dir,
2283
                                janus_videoroom_audiocodec_name(participant->room->acodec), filename);
2284
                        if(participant->arc == NULL) {
2285
                                JANUS_LOG(LOG_ERR, "Couldn't open an audio recording file for this publisher!\n");
2286
                        }
2287
                }
2288
        }
2289
        if(video) {
2290
                memset(filename, 0, 255);
2291
                if(participant->recording_base) {
2292
                        /* Use the filename and path we have been provided */
2293
                        g_snprintf(filename, 255, "%s-video", participant->recording_base);
2294
                        participant->vrc = janus_recorder_create(participant->room->rec_dir,
2295
                                janus_videoroom_videocodec_name(participant->room->vcodec), filename);
2296
                        if(participant->vrc == NULL) {
2297
                                JANUS_LOG(LOG_ERR, "Couldn't open an video recording file for this publisher!\n");
2298
                        }
2299
                } else {
2300
                        /* Build a filename */
2301
                        g_snprintf(filename, 255, "videoroom-%"SCNu64"-user-%"SCNu64"-%"SCNi64"-video",
2302
                                participant->room->room_id, participant->user_id, now);
2303
                        participant->vrc = janus_recorder_create(participant->room->rec_dir,
2304
                                janus_videoroom_videocodec_name(participant->room->vcodec), filename);
2305
                        if(participant->vrc == NULL) {
2306
                                JANUS_LOG(LOG_ERR, "Couldn't open an video recording file for this publisher!\n");
2307
                        }
2308
                }
2309
        }
2310
}
2311

    
2312
static void janus_videoroom_recorder_close(janus_videoroom_participant *participant) {
2313
        if(participant->arc) {
2314
                janus_recorder_close(participant->arc);
2315
                JANUS_LOG(LOG_INFO, "Closed audio recording %s\n", participant->arc->filename ? participant->arc->filename : "??");
2316
                janus_recorder_free(participant->arc);
2317
        }
2318
        participant->arc = NULL;
2319
        if(participant->vrc) {
2320
                janus_recorder_close(participant->vrc);
2321
                JANUS_LOG(LOG_INFO, "Closed video recording %s\n", participant->vrc->filename ? participant->vrc->filename : "??");
2322
                janus_recorder_free(participant->vrc);
2323
        }
2324
        participant->vrc = NULL;
2325
}
2326

    
2327
void janus_videoroom_hangup_media(janus_plugin_session *handle) {
2328
        JANUS_LOG(LOG_INFO, "No WebRTC media anymore\n");
2329
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
2330
                return;
2331
        janus_videoroom_session *session = (janus_videoroom_session *)handle->plugin_handle;        
2332
        if(!session) {
2333
                JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
2334
                return;
2335
        }
2336
        session->started = FALSE;
2337
        if(session->destroyed)
2338
                return;
2339
        if(g_atomic_int_add(&session->hangingup, 1))
2340
                return;
2341
        /* Send an event to the browser and tell the PeerConnection is over */
2342
        if(session->participant_type == janus_videoroom_p_type_publisher) {
2343
                /* This publisher just 'unpublished' */
2344
                janus_videoroom_participant *participant = (janus_videoroom_participant *)session->participant;
2345
                if(participant->sdp)
2346
                        g_free(participant->sdp);
2347
                participant->sdp = NULL;
2348
                participant->firefox = FALSE;
2349
                participant->audio_active = FALSE;
2350
                participant->video_active = FALSE;
2351
                participant->remb_startup = 4;
2352
                participant->remb_latest = 0;
2353
                participant->fir_latest = 0;
2354
                participant->fir_seq = 0;
2355
                /* Get rid of the recorders, if available */
2356
                janus_mutex_lock(&participant->rec_mutex);
2357
                janus_videoroom_recorder_close(participant);
2358
                janus_mutex_unlock(&participant->rec_mutex);
2359
                janus_mutex_lock(&participant->listeners_mutex);
2360
                while(participant->listeners) {
2361
                        janus_videoroom_listener *l = (janus_videoroom_listener *)participant->listeners->data;
2362
                        if(l) {
2363
                                participant->listeners = g_slist_remove(participant->listeners, l);
2364
                                l->feed = NULL;
2365
                        }
2366
                }
2367
                janus_mutex_unlock(&participant->listeners_mutex);
2368
                janus_videoroom_leave_or_unpublish(participant, FALSE);
2369
                /* Also notify event handlers */
2370
                if(participant->room && gateway->events_is_enabled()) {
2371
                        json_t *info = json_object();
2372
                        json_object_set_new(info, "event", json_string("unpublished"));
2373
                        json_object_set_new(info, "room", json_integer(participant->room->room_id));
2374
                        json_object_set_new(info, "id", json_integer(participant->user_id));
2375
                        gateway->notify_event(handle, info);
2376
                }
2377
        } else if(session->participant_type == janus_videoroom_p_type_subscriber) {
2378
                /* Get rid of listener */
2379
                janus_videoroom_listener *listener = (janus_videoroom_listener *)session->participant;
2380
                if(listener) {
2381
                        listener->paused = TRUE;
2382
                        janus_videoroom_participant *publisher = listener->feed;
2383
                        if(publisher != NULL) {
2384
                                janus_mutex_lock(&publisher->listeners_mutex);
2385
                                publisher->listeners = g_slist_remove(publisher->listeners, listener);
2386
                                janus_mutex_unlock(&publisher->listeners_mutex);
2387
                                listener->feed = NULL;
2388
                                /* Also notify event handlers */
2389
                                if(notify_events && gateway->events_is_enabled()) {
2390
                                        json_t *info = json_object();
2391
                                        json_object_set_new(info, "event", json_string("unsubscribed"));
2392
                                        json_object_set_new(info, "room", json_integer(publisher->room->room_id));
2393
                                        json_object_set_new(info, "feed", json_integer(publisher->user_id));
2394
                                        gateway->notify_event(session->handle, info);
2395
                                }
2396
                        }
2397
                }
2398
                /* TODO Should we close the handle as well? */
2399
        } else if(session->participant_type == janus_videoroom_p_type_subscriber_muxed) {
2400
                /* Do the same, but for all sub-listener */
2401
                janus_videoroom_listener_muxed *listener = (janus_videoroom_listener_muxed *)session->participant;
2402
                GSList *ps = listener->listeners;
2403
                while(ps) {
2404
                        janus_videoroom_listener *l = (janus_videoroom_listener *)ps->data;
2405
                        if(l) {
2406
                                l->paused = TRUE;
2407
                                janus_videoroom_participant *publisher = l->feed;
2408
                                if(publisher != NULL) {
2409
                                        janus_mutex_lock(&publisher->listeners_mutex);
2410
                                        publisher->listeners = g_slist_remove(publisher->listeners, l);
2411
                                        janus_mutex_unlock(&publisher->listeners_mutex);
2412
                                        l->feed = NULL;
2413
                                }
2414
                        }
2415
                        /* TODO Should we close the handle as well? */
2416
                        ps = ps->next;
2417
                }
2418
                /* TODO Should we close the handle as well? */
2419
        }
2420
}
2421

    
2422
static void janus_videoroom_sdp_a_format(char *mline, int mline_size, janus_videoroom_audiocodec acodec, int pt, const char *audio_mode) {
2423
        switch(acodec) {
2424
                case JANUS_VIDEOROOM_OPUS:
2425
                        g_snprintf(mline, mline_size, sdp_a_template_opus,
2426
                                pt,                                                /* Opus payload type */
2427
                                audio_mode,
2428
                                pt);                                                 /* Opus payload type */
2429
                        break;
2430
                case JANUS_VIDEOROOM_ISAC_32K:
2431
                        g_snprintf(mline, mline_size, sdp_a_template_isac32,
2432
                                pt,                                                /* ISAC 32K payload type */
2433
                                audio_mode,
2434
                                pt);                                         /* ISAC 32K payload type */
2435
                        break;
2436
                case JANUS_VIDEOROOM_ISAC_16K:
2437
                        g_snprintf(mline, mline_size, sdp_a_template_isac16,
2438
                                pt,                                                /* ISAC 16K payload type */
2439
                                audio_mode,
2440
                                pt);                                                /* ISAC 16K payload type */
2441
                        break;
2442
                case JANUS_VIDEOROOM_PCMU:
2443
                        g_snprintf(mline, mline_size, sdp_a_template_pcmu,
2444
                                pt,                                                /* PCMU payload type */
2445
                                audio_mode,
2446
                                pt);                                                /* PCMU payload type */
2447
                        break;
2448
                case JANUS_VIDEOROOM_PCMA:
2449
                        g_snprintf(mline, mline_size, sdp_a_template_pcma,
2450
                                pt,                                                /* PCMA payload type */
2451
                                audio_mode,
2452
                                pt);                                                /* PCMA payload type */
2453
                        break;
2454
                default:
2455
                        /* Shouldn't happen */
2456
                        mline[0] = '\0';
2457
                        break;
2458
        }
2459
}
2460

    
2461
static void janus_videoroom_sdp_v_format(char *mline, int mline_size, janus_videoroom_videocodec vcodec, int pt, int b, const char *video_mode) {
2462
        switch(vcodec) {
2463
                case JANUS_VIDEOROOM_VP8:
2464
                        g_snprintf(mline, mline_size, sdp_v_template_vp8,
2465
                                pt,                                                        /* payload type */
2466
                                b,                                                                /* Bandwidth */
2467
                                video_mode,                                                /* The publisher gets a recvonly or inactive back */
2468
                                pt,                                                 /* payload type */
2469
                                pt,                                                 /* payload type */
2470
                                pt,                                                 /* payload type */
2471
                                pt,                                                 /* payload type */
2472
                                pt);                                                 /* payload type */
2473
                        break;
2474
                case JANUS_VIDEOROOM_VP9:
2475
                        g_snprintf(mline, mline_size, sdp_v_template_vp9,
2476
                                pt,                                                        /* payload type */
2477
                                b,                                                                /* Bandwidth */
2478
                                video_mode,                                                /* The publisher gets a recvonly or inactive back */
2479
                                pt,                                                 /* payload type */
2480
                                pt,                                                 /* payload type */
2481
                                pt,                                                 /* payload type */
2482
                                pt,                                                 /* payload type */
2483
                                pt);                                                 /* payload type */
2484
                        break;
2485
                case JANUS_VIDEOROOM_H264:
2486
                        g_snprintf(mline, mline_size, sdp_v_template_h264,
2487
                                pt,                                                        /* payload type */
2488
                                b,                                                                /* Bandwidth */
2489
                                video_mode,                                                /* The publisher gets a recvonly or inactive back */
2490
                                pt,                                                 /* payload type */
2491
                                pt,                                                 /* payload type */
2492
                                pt,                                                 /* payload type */
2493
                                pt,                                                 /* payload type */
2494
                                pt,                                                 /* payload type */
2495
                                pt);                                                 /* payload type */
2496
                        break;
2497
                default:
2498
                        /* Shouldn't happen */
2499
                        mline[0] = '\0';
2500
                        break;
2501
        }
2502
}
2503

    
2504
/* Thread to handle incoming messages */
2505
static void *janus_videoroom_handler(void *data) {
2506
        JANUS_LOG(LOG_VERB, "Joining VideoRoom handler thread\n");
2507
        janus_videoroom_message *msg = NULL;
2508
        int error_code = 0;
2509
        char error_cause[512];
2510
        json_t *root = NULL;
2511
        while(g_atomic_int_get(&initialized) && !g_atomic_int_get(&stopping)) {
2512
                msg = g_async_queue_pop(messages);
2513
                if(msg == NULL)
2514
                        continue;
2515
                if(msg == &exit_message)
2516
                        break;
2517
                if(msg->handle == NULL) {
2518
                        janus_videoroom_message_free(msg);
2519
                        continue;
2520
                }
2521
                janus_videoroom_session *session = NULL;
2522
                janus_mutex_lock(&sessions_mutex);
2523
                if(g_hash_table_lookup(sessions, msg->handle) != NULL ) {
2524
                        session = (janus_videoroom_session *)msg->handle->plugin_handle;
2525
                }
2526
                janus_mutex_unlock(&sessions_mutex);
2527
                if(!session) {
2528
                        JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
2529
                        janus_videoroom_message_free(msg);
2530
                        continue;
2531
                }
2532
                if(session->destroyed) {
2533
                        janus_videoroom_message_free(msg);
2534
                        continue;
2535
                }
2536
                /* Handle request */
2537
                error_code = 0;
2538
                root = NULL;
2539
                if(msg->message == NULL) {
2540
                        JANUS_LOG(LOG_ERR, "No message??\n");
2541
                        error_code = JANUS_VIDEOROOM_ERROR_NO_MESSAGE;
2542
                        g_snprintf(error_cause, 512, "%s", "No message??");
2543
                        goto error;
2544
                }
2545
                root = msg->message;
2546
                /* Get the request first */
2547
                JANUS_VALIDATE_JSON_OBJECT(root, request_parameters,
2548
                        error_code, error_cause, TRUE,
2549
                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
2550
                if(error_code != 0)
2551
                        goto error;
2552
                json_t *request = json_object_get(root, "request");
2553
                const char *request_text = json_string_value(request);
2554
                json_t *event = NULL;
2555
                /* 'create' and 'destroy' are handled synchronously: what kind of participant is this session referring to? */
2556
                if(session->participant_type == janus_videoroom_p_type_none) {
2557
                        JANUS_LOG(LOG_VERB, "Configuring new participant\n");
2558
                        /* Not configured yet, we need to do this now */
2559
                        if(strcasecmp(request_text, "join") && strcasecmp(request_text, "joinandconfigure")) {
2560
                                JANUS_LOG(LOG_ERR, "Invalid request on unconfigured participant\n");
2561
                                error_code = JANUS_VIDEOROOM_ERROR_JOIN_FIRST;
2562
                                g_snprintf(error_cause, 512, "Invalid request on unconfigured participant");
2563
                                goto error;
2564
                        }
2565
                        JANUS_VALIDATE_JSON_OBJECT(root, join_parameters,
2566
                                error_code, error_cause, TRUE,
2567
                                JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
2568
                        if(error_code != 0)
2569
                                goto error;
2570
                        janus_mutex_lock(&rooms_mutex);
2571
                        janus_videoroom *videoroom = NULL;
2572
                        error_code = janus_videoroom_access_room(root, FALSE, TRUE, &videoroom, error_cause, sizeof(error_cause));
2573
                        janus_mutex_unlock(&rooms_mutex);
2574
                        if(error_code != 0)
2575
                                goto error;
2576
                        json_t *ptype = json_object_get(root, "ptype");
2577
                        const char *ptype_text = json_string_value(ptype);
2578
                        if(!strcasecmp(ptype_text, "publisher")) {
2579
                                JANUS_LOG(LOG_VERB, "Configuring new publisher\n");
2580
                                JANUS_VALIDATE_JSON_OBJECT(root, publisher_parameters,
2581
                                        error_code, error_cause, TRUE,
2582
                                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
2583
                                if(error_code != 0)
2584
                                        goto error;
2585
                                json_t *display = json_object_get(root, "display");
2586
                                const char *display_text = display ? json_string_value(display) : NULL;
2587
                                guint64 user_id = 0;
2588
                                json_t *id = json_object_get(root, "id");
2589
                                if(id) {
2590
                                        user_id = json_integer_value(id);
2591
                                        janus_mutex_lock(&videoroom->participants_mutex);
2592
                                        if(g_hash_table_lookup(videoroom->participants, &user_id) != NULL) {
2593
                                                janus_mutex_unlock(&videoroom->participants_mutex);
2594
                                                /* User ID already taken */
2595
                                                JANUS_LOG(LOG_ERR, "User ID %"SCNu64" already exists\n", user_id);
2596
                                                error_code = JANUS_VIDEOROOM_ERROR_ID_EXISTS;
2597
                                                g_snprintf(error_cause, 512, "User ID %"SCNu64" already exists", user_id);
2598
                                                goto error;
2599
                                        }
2600
                                        janus_mutex_unlock(&videoroom->participants_mutex);
2601
                                }
2602
                                if(user_id == 0) {
2603
                                        /* Generate a random ID */
2604
                                        janus_mutex_lock(&videoroom->participants_mutex);
2605
                                        while(user_id == 0) {
2606
                                                user_id = janus_random_uint64();
2607
                                                if(g_hash_table_lookup(videoroom->participants, &user_id) != NULL) {
2608
                                                        /* User ID already taken, try another one */
2609
                                                        user_id = 0;
2610
                                                }
2611
                                        }
2612
                                        janus_mutex_unlock(&videoroom->participants_mutex);
2613
                                }
2614
                                JANUS_LOG(LOG_VERB, "  -- Publisher ID: %"SCNu64"\n", user_id);
2615
                                json_t *audio = NULL, *video = NULL, *bitrate = NULL, *record = NULL, *recfile = NULL;
2616
                                if(!strcasecmp(request_text, "joinandconfigure")) {
2617
                                        /* Also configure (or publish a new feed) audio/video/bitrate for this new publisher */
2618
                                        /* join_parameters were validated earlier. */
2619
                                        audio = json_object_get(root, "audio");
2620
                                        video = json_object_get(root, "video");
2621
                                        bitrate = json_object_get(root, "bitrate");
2622
                                        record = json_object_get(root, "record");
2623
                                        recfile = json_object_get(root, "filename");
2624
                                }
2625
                                janus_videoroom_participant *publisher = g_malloc0(sizeof(janus_videoroom_participant));
2626
                                if(publisher == NULL) {
2627
                                        JANUS_LOG(LOG_FATAL, "Memory error!\n");
2628
                                        error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;
2629
                                        g_snprintf(error_cause, 512, "Memory error");
2630
                                        goto error;
2631
                                }
2632
                                publisher->session = session;
2633
                                publisher->room = videoroom;
2634
                                publisher->user_id = user_id;
2635
                                publisher->display = display_text ? g_strdup(display_text) : NULL;
2636
                                publisher->sdp = NULL;                /* We'll deal with this later */
2637
                                publisher->audio = FALSE;        /* We'll deal with this later */
2638
                                publisher->video = FALSE;        /* We'll deal with this later */
2639
                                publisher->data = FALSE;        /* We'll deal with this later */
2640
                                publisher->audio_active = FALSE;
2641
                                publisher->video_active = FALSE;
2642
                                publisher->recording_active = FALSE;
2643
                                publisher->recording_base = NULL;
2644
                                publisher->arc = NULL;
2645
                                publisher->vrc = NULL;
2646
                                janus_mutex_init(&publisher->rec_mutex);
2647
                                publisher->firefox = FALSE;
2648
                                publisher->bitrate = videoroom->bitrate;
2649
                                publisher->listeners = NULL;
2650
                                janus_mutex_init(&publisher->listeners_mutex);
2651
                                publisher->audio_pt = OPUS_PT;
2652
                                switch(videoroom->acodec) {
2653
                                        case JANUS_VIDEOROOM_OPUS:
2654
                                                publisher->audio_pt = OPUS_PT;
2655
                                                break;
2656
                                        case JANUS_VIDEOROOM_ISAC_32K:
2657
                                                publisher->audio_pt = ISAC32_PT;
2658
                                                break;
2659
                                        case JANUS_VIDEOROOM_ISAC_16K:
2660
                                                publisher->audio_pt = ISAC16_PT;
2661
                                                break;
2662
                                        case JANUS_VIDEOROOM_PCMU:
2663
                                                publisher->audio_pt = PCMU_PT;
2664
                                                break;
2665
                                        case JANUS_VIDEOROOM_PCMA:
2666
                                                publisher->audio_pt = PCMA_PT;
2667
                                                break;
2668
                                        default:
2669
                                                /* Shouldn't happen */
2670
                                                publisher->audio_pt = OPUS_PT;
2671
                                                break;
2672
                                }
2673
                                switch(videoroom->vcodec) {
2674
                                        case JANUS_VIDEOROOM_VP8:
2675
                                                publisher->video_pt = VP8_PT;
2676
                                                break;
2677
                                        case JANUS_VIDEOROOM_VP9:
2678
                                                publisher->video_pt = VP9_PT;
2679
                                                break;
2680
                                        case JANUS_VIDEOROOM_H264:
2681
                                                publisher->video_pt = H264_PT;
2682
                                                break;
2683
                                        default:
2684
                                                /* Shouldn't happen */
2685
                                                publisher->video_pt = VP8_PT;
2686
                                                break;
2687
                                }
2688
                                publisher->audio_ssrc = janus_random_uint32();
2689
                                publisher->video_ssrc = janus_random_uint32();
2690
                                publisher->remb_startup = 4;
2691
                                publisher->remb_latest = 0;
2692
                                publisher->fir_latest = 0;
2693
                                publisher->fir_seq = 0;
2694
                                janus_mutex_init(&publisher->rtp_forwarders_mutex);
2695
                                publisher->rtp_forwarders = g_hash_table_new_full(NULL, NULL, NULL, (GDestroyNotify)janus_rtp_forwarder_free_helper);
2696
                                publisher->udp_sock = -1;
2697
                                /* In case we also wanted to configure */
2698
                                if(audio) {
2699
                                        publisher->audio_active = json_is_true(audio);
2700
                                        JANUS_LOG(LOG_VERB, "Setting audio property: %s (room %"SCNu64", user %"SCNu64")\n", publisher->audio_active ? "true" : "false", publisher->room->room_id, publisher->user_id);
2701
                                }
2702
                                if(video) {
2703
                                        publisher->video_active = json_is_true(video);
2704
                                        JANUS_LOG(LOG_VERB, "Setting video property: %s (room %"SCNu64", user %"SCNu64")\n", publisher->video_active ? "true" : "false", publisher->room->room_id, publisher->user_id);
2705
                                }
2706
                                if(bitrate) {
2707
                                        publisher->bitrate = json_integer_value(bitrate);
2708
                                        JANUS_LOG(LOG_VERB, "Setting video bitrate: %"SCNu64" (room %"SCNu64", user %"SCNu64")\n", publisher->bitrate, publisher->room->room_id, publisher->user_id);
2709
                                }
2710
                                if(record) {
2711
                                        publisher->recording_active = json_is_true(record);
2712
                                        JANUS_LOG(LOG_VERB, "Setting record property: %s (room %"SCNu64", user %"SCNu64")\n", publisher->recording_active ? "true" : "false", publisher->room->room_id, publisher->user_id);
2713
                                }
2714
                                if(recfile) {
2715
                                        publisher->recording_base = g_strdup(json_string_value(recfile));
2716
                                        JANUS_LOG(LOG_VERB, "Setting recording basename: %s (room %"SCNu64", user %"SCNu64")\n", publisher->recording_base, publisher->room->room_id, publisher->user_id);
2717
                                }
2718
                                /* Done */
2719
                                session->participant_type = janus_videoroom_p_type_publisher;
2720
                                session->participant = publisher;
2721
                                /* Return a list of all available publishers (those with an SDP available, that is) */
2722
                                json_t *list = json_array();
2723
                                GHashTableIter iter;
2724
                                gpointer value;
2725
                                janus_mutex_lock(&videoroom->participants_mutex);
2726
                                g_hash_table_insert(videoroom->participants, janus_uint64_dup(publisher->user_id), publisher);
2727
                                g_hash_table_iter_init(&iter, videoroom->participants);
2728
                                while (!videoroom->destroyed && g_hash_table_iter_next(&iter, NULL, &value)) {
2729
                                        janus_videoroom_participant *p = value;
2730
                                        if(p == publisher || !p->sdp || !p->session->started) {
2731
                                                continue;
2732
                                        }
2733
                                        json_t *pl = json_object();
2734
                                        json_object_set_new(pl, "id", json_integer(p->user_id));
2735
                                        if(p->display)
2736
                                                json_object_set_new(pl, "display", json_string(p->display));
2737
                                        json_array_append_new(list, pl);
2738
                                }
2739
                                janus_mutex_unlock(&videoroom->participants_mutex);
2740
                                event = json_object();
2741
                                json_object_set_new(event, "videoroom", json_string("joined"));
2742
                                json_object_set_new(event, "room", json_integer(videoroom->room_id));
2743
                                json_object_set_new(event, "description", json_string(videoroom->room_name));
2744
                                json_object_set_new(event, "id", json_integer(user_id));
2745
                                json_object_set_new(event, "publishers", list);
2746
                                /* Also notify event handlers */
2747
                                if(notify_events && gateway->events_is_enabled()) {
2748
                                        json_t *info = json_object();
2749
                                        json_object_set_new(info, "event", json_string("joined"));
2750
                                        json_object_set_new(info, "room", json_integer(videoroom->room_id));
2751
                                        json_object_set_new(info, "id", json_integer(user_id));
2752
                                        if(display_text != NULL)
2753
                                                json_object_set_new(info, "display", json_string(display_text));
2754
                                        gateway->notify_event(session->handle, info);
2755
                                }
2756
                        } else if(!strcasecmp(ptype_text, "listener")) {
2757
                                JANUS_LOG(LOG_VERB, "Configuring new listener\n");
2758
                                /* This is a new listener */
2759
                                JANUS_VALIDATE_JSON_OBJECT(root, listener_parameters,
2760
                                        error_code, error_cause, TRUE,
2761
                                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
2762
                                if(error_code != 0)
2763
                                        goto error;
2764
                                json_t *feed = json_object_get(root, "feed");
2765
                                guint64 feed_id = json_integer_value(feed);
2766
                                json_t *audio = json_object_get(root, "audio");
2767
                                json_t *video = json_object_get(root, "video");
2768
                                json_t *data = json_object_get(root, "data");
2769
                                janus_mutex_lock(&videoroom->participants_mutex);
2770
                                janus_videoroom_participant *publisher = g_hash_table_lookup(videoroom->participants, &feed_id);
2771
                                janus_mutex_unlock(&videoroom->participants_mutex);
2772
                                if(publisher == NULL || publisher->sdp == NULL) {
2773
                                        JANUS_LOG(LOG_ERR, "No such feed (%"SCNu64")\n", feed_id);
2774
                                        error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_FEED;
2775
                                        g_snprintf(error_cause, 512, "No such feed (%"SCNu64")", feed_id);
2776
                                        goto error;
2777
                                } else {
2778
                                        janus_videoroom_listener *listener = g_malloc0(sizeof(janus_videoroom_listener));
2779
                                        if(listener == NULL) {
2780
                                                JANUS_LOG(LOG_FATAL, "Memory error!\n");
2781
                                                error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;
2782
                                                g_snprintf(error_cause, 512, "Memory error");
2783
                                                goto error;
2784
                                        }
2785
                                        listener->session = session;
2786
                                        listener->room = videoroom;
2787
                                        listener->feed = publisher;
2788
                                        /* Initialize the listener context */
2789
                                        listener->context.a_last_ssrc = 0;
2790
                                        listener->context.a_last_ssrc = 0;
2791
                                        listener->context.a_last_ts = 0;
2792
                                        listener->context.a_base_ts = 0;
2793
                                        listener->context.a_base_ts_prev = 0;
2794
                                        listener->context.v_last_ssrc = 0;
2795
                                        listener->context.v_last_ts = 0;
2796
                                        listener->context.v_base_ts = 0;
2797
                                        listener->context.v_base_ts_prev = 0;
2798
                                        listener->context.a_last_seq = 0;
2799
                                        listener->context.a_base_seq = 0;
2800
                                        listener->context.a_base_seq_prev = 0;
2801
                                        listener->context.v_last_seq = 0;
2802
                                        listener->context.v_base_seq = 0;
2803
                                        listener->context.v_base_seq_prev = 0;
2804
                                        listener->audio = audio ? json_is_true(audio) : TRUE;        /* True by default */
2805
                                        if(!publisher->audio)
2806
                                                listener->audio = FALSE;        /* ... unless the publisher isn't sending any audio */
2807
                                        listener->video = video ? json_is_true(video) : TRUE;        /* True by default */
2808
                                        if(!publisher->video)
2809
                                                listener->video = FALSE;        /* ... unless the publisher isn't sending any video */
2810
                                        listener->data = data ? json_is_true(data) : TRUE;        /* True by default */
2811
                                        if(!publisher->data)
2812
                                                listener->data = FALSE;        /* ... unless the publisher isn't sending any data */
2813
                                        listener->paused = TRUE;        /* We need an explicit start from the listener */
2814
                                        listener->parent = NULL;
2815
                                        session->participant = listener;
2816
                                        janus_mutex_lock(&publisher->listeners_mutex);
2817
                                        publisher->listeners = g_slist_append(publisher->listeners, listener);
2818
                                        janus_mutex_unlock(&publisher->listeners_mutex);
2819
                                        event = json_object();
2820
                                        json_object_set_new(event, "videoroom", json_string("attached"));
2821
                                        json_object_set_new(event, "room", json_integer(videoroom->room_id));
2822
                                        json_object_set_new(event, "id", json_integer(feed_id));
2823
                                        if(publisher->display)
2824
                                                json_object_set_new(event, "display", json_string(publisher->display));
2825
                                        session->participant_type = janus_videoroom_p_type_subscriber;
2826
                                        JANUS_LOG(LOG_VERB, "Preparing JSON event as a reply\n");
2827
                                        char *event_text = json_dumps(event, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
2828
                                        json_decref(event);
2829
                                        /* Negotiate by sending the selected publisher SDP back */
2830
                                        if(publisher->sdp != NULL) {
2831
                                                /* How long will the gateway take to push the event? */
2832
                                                g_atomic_int_set(&session->hangingup, 0);
2833
                                                gint64 start = janus_get_monotonic_time();
2834
                                                int res = gateway->push_event(msg->handle, &janus_videoroom_plugin, msg->transaction, event_text, "offer", publisher->sdp);
2835
                                                JANUS_LOG(LOG_VERB, "  >> Pushing event: %d (took %"SCNu64" us)\n", res, janus_get_monotonic_time()-start);
2836
                                                JANUS_LOG(LOG_VERB, "  >> %d\n", res);
2837
                                                g_free(event_text);
2838
                                                root = NULL;
2839
                                                janus_videoroom_message_free(msg);
2840
                                                /* Also notify event handlers */
2841
                                                if(notify_events && gateway->events_is_enabled()) {
2842
                                                        json_t *info = json_object();
2843
                                                        json_object_set_new(info, "event", json_string("subscribing"));
2844
                                                        json_object_set_new(info, "room", json_integer(videoroom->room_id));
2845
                                                        json_object_set_new(info, "feed", json_integer(feed_id));
2846
                                                        gateway->notify_event(session->handle, info);
2847
                                                }
2848
                                                continue;
2849
                                        }
2850
                                        g_free(event_text);
2851
                                }
2852
                        } else if(!strcasecmp(ptype_text, "muxed-listener")) {
2853
                                /* This is a new Multiplexed listener */
2854
                                JANUS_LOG(LOG_INFO, "Configuring new Multiplexed listener\n");
2855
                                /* Any feed we want to attach to already? */
2856
                                GList *list = NULL;
2857
                                JANUS_VALIDATE_JSON_OBJECT(root, feeds_parameters,
2858
                                        error_code, error_cause, TRUE,
2859
                                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
2860
                                if(error_code != 0)
2861
                                        goto error;
2862
                                json_t *feeds = json_object_get(root, "feeds");
2863
                                if(feeds && json_array_size(feeds) > 0) {
2864
                                        unsigned int i = 0;
2865
                                        int problem = 0;
2866
                                        for(i=0; i<json_array_size(feeds); i++) {
2867
                                                if(videoroom->destroyed) {
2868
                                                        problem = 1;
2869
                                                        JANUS_LOG(LOG_ERR, "Room destroyed");
2870
                                                        error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_ROOM;
2871
                                                        g_snprintf(error_cause, 512, "Room destroyed");
2872
                                                        break;
2873
                                                }
2874
                                                json_t *feed = json_array_get(feeds, i);
2875
                                                if(!feed || !json_is_integer(feed)) {
2876
                                                        problem = 1;
2877
                                                        JANUS_LOG(LOG_ERR, "Invalid element (feeds in the array must be integers)\n");
2878
                                                        error_code = JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT;
2879
                                                        g_snprintf(error_cause, 512, "Invalid element (feeds in the array must be integers)");
2880
                                                        break;
2881
                                                }
2882
                                                uint64_t feed_id = json_integer_value(feed);
2883
                                                janus_mutex_lock(&videoroom->participants_mutex);
2884
                                                janus_videoroom_participant *publisher = g_hash_table_lookup(videoroom->participants, &feed_id);
2885
                                                janus_mutex_unlock(&videoroom->participants_mutex);
2886
                                                if(publisher == NULL) { //~ || publisher->sdp == NULL) {
2887
                                                        /* FIXME For muxed listeners, we accept subscriptions to existing participants who haven't published yet */
2888
                                                        problem = 1;
2889
                                                        JANUS_LOG(LOG_ERR, "No such feed (%"SCNu64")\n", feed_id);
2890
                                                        error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_FEED;
2891
                                                        g_snprintf(error_cause, 512, "No such feed (%"SCNu64")", feed_id);
2892
                                                        break;
2893
                                                }
2894
                                                list = g_list_prepend(list, GUINT_TO_POINTER(feed_id));
2895
                                                JANUS_LOG(LOG_INFO, "  -- Subscribing to feed %"SCNu64"\n", feed_id);
2896
                                        }
2897
                                        if(problem) {
2898
                                                goto error;
2899
                                        }
2900
                                }
2901
                                /* Allocate listener */
2902
                                janus_videoroom_listener_muxed *listener = g_malloc0(sizeof(janus_videoroom_listener_muxed));
2903
                                if(listener == NULL) {
2904
                                        JANUS_LOG(LOG_FATAL, "Memory error!\n");
2905
                                        error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;
2906
                                        g_snprintf(error_cause, 512, "Memory error");
2907
                                        goto error;
2908
                                }
2909
                                listener->session = session;
2910
                                listener->room = videoroom;
2911
                                session->participant_type = janus_videoroom_p_type_subscriber_muxed;
2912
                                session->participant = listener;
2913
                                /* Ack that we created the listener */
2914
                                event = json_object();
2915
                                json_object_set_new(event, "videoroom", json_string("muxed-created"));
2916
                                json_object_set_new(event, "room", json_integer(videoroom->room_id));
2917
                                JANUS_LOG(LOG_VERB, "Preparing JSON event as a reply\n");
2918
                                char *event_text = json_dumps(event, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
2919
                                json_decref(event);
2920
                                /* How long will the gateway take to push the event? */
2921
                                gint64 start = janus_get_monotonic_time();
2922
                                int res = gateway->push_event(msg->handle, &janus_videoroom_plugin, msg->transaction, event_text, NULL, NULL);
2923
                                JANUS_LOG(LOG_VERB, "  >> Pushing event: %d (took %"SCNu64" us)\n", res, janus_get_monotonic_time()-start);
2924
                                JANUS_LOG(LOG_VERB, "  >> %d\n", res);
2925
                                g_free(event_text);
2926
                                root = NULL;
2927
                                /* Attach to feeds if needed */
2928
                                if(list != NULL) {
2929
                                        JANUS_LOG(LOG_INFO, "Subscribing to %d feeds\n", g_list_length(list));
2930
                                        list = g_list_reverse(list);
2931
                                        if(videoroom->destroyed || janus_videoroom_muxed_subscribe(listener, list, msg->transaction) < 0) {
2932
                                                JANUS_LOG(LOG_ERR, "Error subscribing!\n");
2933
                                                error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;        /* FIXME */
2934
                                                g_snprintf(error_cause, 512, "Error subscribing!");
2935
                                                goto error;
2936
                                        }
2937
                                }
2938
                                janus_videoroom_message_free(msg);
2939
                                continue;
2940
                        } else {
2941
                                JANUS_LOG(LOG_ERR, "Invalid element (ptype)\n");
2942
                                error_code = JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT;
2943
                                g_snprintf(error_cause, 512, "Invalid element (ptype)");
2944
                                goto error;
2945
                        }
2946
                } else if(session->participant_type == janus_videoroom_p_type_publisher) {
2947
                        /* Handle this publisher */
2948
                        janus_videoroom_participant *participant = (janus_videoroom_participant *)session->participant;
2949
                        if(participant == NULL) {
2950
                                JANUS_LOG(LOG_ERR, "Invalid participant instance\n");
2951
                                error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;
2952
                                g_snprintf(error_cause, 512, "Invalid participant instance");
2953
                                goto error;
2954
                        }
2955
                        if(!strcasecmp(request_text, "join") || !strcasecmp(request_text, "joinandconfigure")) {
2956
                                JANUS_LOG(LOG_ERR, "Already in as a publisher on this handle\n");
2957
                                error_code = JANUS_VIDEOROOM_ERROR_ALREADY_JOINED;
2958
                                g_snprintf(error_cause, 512, "Already in as a publisher on this handle");
2959
                                goto error;
2960
                        } else if(!strcasecmp(request_text, "configure") || !strcasecmp(request_text, "publish")) {
2961
                                if(!strcasecmp(request_text, "publish") && participant->sdp) {
2962
                                        JANUS_LOG(LOG_ERR, "Can't publish, already published\n");
2963
                                        error_code = JANUS_VIDEOROOM_ERROR_ALREADY_PUBLISHED;
2964
                                        g_snprintf(error_cause, 512, "Can't publish, already published");
2965
                                        goto error;
2966
                                }
2967
                                /* Configure (or publish a new feed) audio/video/bitrate for this publisher */
2968
                                JANUS_VALIDATE_JSON_OBJECT(root, publish_parameters,
2969
                                        error_code, error_cause, TRUE,
2970
                                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
2971
                                if(error_code != 0)
2972
                                        goto error;
2973
                                json_t *audio = json_object_get(root, "audio");
2974
                                json_t *video = json_object_get(root, "video");
2975
                                json_t *bitrate = json_object_get(root, "bitrate");
2976
                                json_t *record = json_object_get(root, "record");
2977
                                json_t *recfile = json_object_get(root, "filename");
2978
                                if(audio) {
2979
                                        participant->audio_active = json_is_true(audio);
2980
                                        JANUS_LOG(LOG_VERB, "Setting audio property: %s (room %"SCNu64", user %"SCNu64")\n", participant->audio_active ? "true" : "false", participant->room->room_id, participant->user_id);
2981
                                }
2982
                                if(video) {
2983
                                        participant->video_active = json_is_true(video);
2984
                                        JANUS_LOG(LOG_VERB, "Setting video property: %s (room %"SCNu64", user %"SCNu64")\n", participant->video_active ? "true" : "false", participant->room->room_id, participant->user_id);
2985
                                }
2986
                                if(bitrate) {
2987
                                        participant->bitrate = json_integer_value(bitrate);
2988
                                        JANUS_LOG(LOG_VERB, "Setting video bitrate: %"SCNu64" (room %"SCNu64", user %"SCNu64")\n", participant->bitrate, participant->room->room_id, participant->user_id);
2989
                                        /* Send a new REMB */
2990
                                        if(session->started)
2991
                                                participant->remb_latest = janus_get_monotonic_time();
2992
                                        char rtcpbuf[24];
2993
                                        janus_rtcp_remb((char *)(&rtcpbuf), 24, participant->bitrate ? participant->bitrate : 256*1024);
2994
                                        gateway->relay_rtcp(msg->handle, 1, rtcpbuf, 24);
2995
                                }
2996
                                janus_mutex_lock(&participant->rec_mutex);
2997
                                gboolean prev_recording_active = participant->recording_active;
2998
                                if(record) {
2999
                                        participant->recording_active = json_is_true(record);
3000
                                        JANUS_LOG(LOG_VERB, "Setting record property: %s (room %"SCNu64", user %"SCNu64")\n", participant->recording_active ? "true" : "false", participant->room->room_id, participant->user_id);
3001
                                }
3002
                                if(recfile) {
3003
                                        participant->recording_base = g_strdup(json_string_value(recfile));
3004
                                        JANUS_LOG(LOG_VERB, "Setting recording basename: %s (room %"SCNu64", user %"SCNu64")\n", participant->recording_base, participant->room->room_id, participant->user_id);
3005
                                }
3006
                                /* Do we need to do something with the recordings right now? */
3007
                                if(participant->recording_active != prev_recording_active) {
3008
                                        /* Something changed */
3009
                                        if(!participant->recording_active) {
3010
                                                /* Not recording (anymore?) */
3011
                                                janus_videoroom_recorder_close(participant);
3012
                                        } else if(participant->recording_active && participant->sdp) {
3013
                                                /* We've started recording, send a PLI/FIR and go on */
3014
                                                janus_videoroom_recorder_create(participant, strstr(participant->sdp, "m=audio") != NULL, strstr(participant->sdp, "m=video") != NULL);
3015
                                                if(strstr(participant->sdp, "m=video")) {
3016
                                                        /* Send a FIR */
3017
                                                        char buf[20];
3018
                                                        memset(buf, 0, 20);
3019
                                                        janus_rtcp_fir((char *)&buf, 20, &participant->fir_seq);
3020
                                                        JANUS_LOG(LOG_VERB, "Recording video, sending FIR to %"SCNu64" (%s)\n",
3021
                                                                participant->user_id, participant->display ? participant->display : "??");
3022
                                                        gateway->relay_rtcp(participant->session->handle, 1, buf, 20);
3023
                                                        /* Send a PLI too, just in case... */
3024
                                                        memset(buf, 0, 12);
3025
                                                        janus_rtcp_pli((char *)&buf, 12);
3026
                                                        JANUS_LOG(LOG_VERB, "Recording video, sending PLI to %"SCNu64" (%s)\n",
3027
                                                                participant->user_id, participant->display ? participant->display : "??");
3028
                                                        gateway->relay_rtcp(participant->session->handle, 1, buf, 12);
3029
                                                }
3030
                                        }
3031
                                }
3032
                                janus_mutex_unlock(&participant->rec_mutex);
3033
                                /* Done */
3034
                                event = json_object();
3035
                                json_object_set_new(event, "videoroom", json_string("event"));
3036
                                json_object_set_new(event, "room", json_integer(participant->room->room_id));
3037
                                json_object_set_new(event, "configured", json_string("ok"));
3038
                                /* Also notify event handlers */
3039
                                if(notify_events && gateway->events_is_enabled()) {
3040
                                        json_t *info = json_object();
3041
                                        json_object_set_new(info, "event", json_string("configured"));
3042
                                        json_object_set_new(info, "room", json_integer(participant->room->room_id));
3043
                                        json_object_set_new(info, "id", json_integer(participant->user_id));
3044
                                        json_object_set_new(info, "audio_active", participant->audio_active ? json_true() : json_false());
3045
                                        json_object_set_new(info, "video_active", participant->video_active ? json_true() : json_false());
3046
                                        json_object_set_new(info, "bitrate", json_integer(participant->bitrate));
3047
                                        if(participant->arc || participant->vrc) {
3048
                                                json_t *recording = json_object();
3049
                                                if(participant->arc && participant->arc->filename)
3050
                                                        json_object_set_new(recording, "audio", json_string(participant->arc->filename));
3051
                                                if(participant->vrc && participant->vrc->filename)
3052
                                                        json_object_set_new(recording, "video", json_string(participant->vrc->filename));
3053
                                                json_object_set_new(info, "recording", recording);
3054
                                        }
3055
                                        gateway->notify_event(session->handle, info);
3056
                                }
3057
                        } else if(!strcasecmp(request_text, "unpublish")) {
3058
                                /* This participant wants to unpublish */
3059
                                if(!participant->sdp) {
3060
                                        JANUS_LOG(LOG_ERR, "Can't unpublish, not published\n");
3061
                                        error_code = JANUS_VIDEOROOM_ERROR_NOT_PUBLISHED;
3062
                                        g_snprintf(error_cause, 512, "Can't unpublish, not published");
3063
                                        goto error;
3064
                                }
3065
                                /* Tell the core to tear down the PeerConnection, hangup_media will do the rest */
3066
                                gateway->close_pc(session->handle);
3067
                                /* Done */
3068
                                event = json_object();
3069
                                json_object_set_new(event, "videoroom", json_string("event"));
3070
                                json_object_set_new(event, "room", json_integer(participant->room->room_id));
3071
                                json_object_set_new(event, "unpublished", json_string("ok"));
3072
                        } else if(!strcasecmp(request_text, "leave")) {
3073
                                /* This publisher is leaving, tell everybody */
3074
                                janus_videoroom_leave_or_unpublish(participant, TRUE);
3075
                                /* Done */
3076
                                participant->audio_active = FALSE;
3077
                                participant->video_active = FALSE;
3078
                                session->started = FALSE;
3079
                                //~ session->destroy = TRUE;
3080
                        } else {
3081
                                JANUS_LOG(LOG_ERR, "Unknown request '%s'\n", request_text);
3082
                                error_code = JANUS_VIDEOROOM_ERROR_INVALID_REQUEST;
3083
                                g_snprintf(error_cause, 512, "Unknown request '%s'", request_text);
3084
                                goto error;
3085
                        }
3086
                } else if(session->participant_type == janus_videoroom_p_type_subscriber) {
3087
                        /* Handle this listener */
3088
                        janus_videoroom_listener *listener = (janus_videoroom_listener *)session->participant;
3089
                        if(listener == NULL) {
3090
                                JANUS_LOG(LOG_ERR, "Invalid listener instance\n");
3091
                                error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;
3092
                                g_snprintf(error_cause, 512, "Invalid listener instance");
3093
                                goto error;
3094
                        }
3095
                        if(!strcasecmp(request_text, "join")) {
3096
                                JANUS_LOG(LOG_ERR, "Already in as a listener on this handle\n");
3097
                                error_code = JANUS_VIDEOROOM_ERROR_ALREADY_JOINED;
3098
                                g_snprintf(error_cause, 512, "Already in as a listener on this handle");
3099
                                goto error;
3100
                        } else if(!strcasecmp(request_text, "start")) {
3101
                                /* Start/restart receiving the publisher streams */
3102
                                janus_videoroom_participant *publisher = listener->feed;
3103
                                listener->paused = FALSE;
3104
                                event = json_object();
3105
                                json_object_set_new(event, "videoroom", json_string("event"));
3106
                                json_object_set_new(event, "room", json_integer(listener->room->room_id));
3107
                                json_object_set_new(event, "started", json_string("ok"));
3108
                                if(publisher) {
3109
                                        /* Send a FIR */
3110
                                        char buf[20];
3111
                                        memset(buf, 0, 20);
3112
                                        janus_rtcp_fir((char *)&buf, 20, &publisher->fir_seq);
3113
                                        JANUS_LOG(LOG_VERB, "Resuming publisher, sending FIR to %"SCNu64" (%s)\n", publisher->user_id, publisher->display ? publisher->display : "??");
3114
                                        gateway->relay_rtcp(publisher->session->handle, 1, buf, 20);
3115
                                        /* Send a PLI too, just in case... */
3116
                                        memset(buf, 0, 12);
3117
                                        janus_rtcp_pli((char *)&buf, 12);
3118
                                        JANUS_LOG(LOG_VERB, "Resuming publisher, sending PLI to %"SCNu64" (%s)\n", publisher->user_id, publisher->display ? publisher->display : "??");
3119
                                        gateway->relay_rtcp(publisher->session->handle, 1, buf, 12);
3120
                                }
3121
                        } else if(!strcasecmp(request_text, "configure")) {
3122
                                JANUS_VALIDATE_JSON_OBJECT(root, configure_parameters,
3123
                                        error_code, error_cause, TRUE,
3124
                                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
3125
                                if(error_code != 0)
3126
                                        goto error;
3127
                                json_t *audio = json_object_get(root, "audio");
3128
                                json_t *video = json_object_get(root, "video");
3129
                                json_t *data = json_object_get(root, "data");
3130
                                /* Update the audio/video/data flags, if set */
3131
                                janus_videoroom_participant *publisher = listener->feed;
3132
                                if(publisher) {
3133
                                        if(audio && publisher->audio)
3134
                                                listener->audio = json_is_true(audio);
3135
                                        if(video && publisher->video)
3136
                                                listener->video = json_is_true(video);
3137
                                        if(data && publisher->data)
3138
                                                listener->data = json_is_true(data);
3139
                                }
3140
                                event = json_object();
3141
                                json_object_set_new(event, "videoroom", json_string("event"));
3142
                                json_object_set_new(event, "room", json_integer(listener->room->room_id));
3143
                                json_object_set_new(event, "configured", json_string("ok"));
3144
                        } else if(!strcasecmp(request_text, "pause")) {
3145
                                /* Stop receiving the publisher streams for a while */
3146
                                listener->paused = TRUE;
3147
                                event = json_object();
3148
                                json_object_set_new(event, "videoroom", json_string("event"));
3149
                                json_object_set_new(event, "room", json_integer(listener->room->room_id));
3150
                                json_object_set_new(event, "paused", json_string("ok"));
3151
                        } else if(!strcasecmp(request_text, "switch")) {
3152
                                /* This listener wants to switch to a different publisher */
3153
                                JANUS_VALIDATE_JSON_OBJECT(root, listener_parameters,
3154
                                        error_code, error_cause, TRUE,
3155
                                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
3156
                                if(error_code != 0)
3157
                                        goto error;
3158
                                json_t *feed = json_object_get(root, "feed");
3159
                                guint64 feed_id = json_integer_value(feed);
3160
                                json_t *audio = json_object_get(root, "audio");
3161
                                json_t *video = json_object_get(root, "video");
3162
                                json_t *data = json_object_get(root, "data");
3163
                                if(!listener->room) {
3164
                                        JANUS_LOG(LOG_ERR, "Room Destroyed \n");
3165
                                        error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_ROOM;
3166
                                        g_snprintf(error_cause, 512, "No such room ");
3167
                                        goto error;
3168
                                }
3169
                                if(listener->room->destroyed) {
3170
                                        JANUS_LOG(LOG_ERR, "Room Destroyed (%"SCNu64")\n", listener->room->room_id);
3171
                                        error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_ROOM;
3172
                                        g_snprintf(error_cause, 512, "No such room (%"SCNu64")", listener->room->room_id);
3173
                                        goto error;
3174
                                }
3175
                                janus_mutex_lock(&listener->room->participants_mutex);
3176
                                janus_videoroom_participant *publisher = g_hash_table_lookup(listener->room->participants, &feed_id);
3177
                                janus_mutex_unlock(&listener->room->participants_mutex);
3178
                                if(publisher == NULL || publisher->sdp == NULL) {
3179
                                        JANUS_LOG(LOG_ERR, "No such feed (%"SCNu64")\n", feed_id);
3180
                                        error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_FEED;
3181
                                        g_snprintf(error_cause, 512, "No such feed (%"SCNu64")", feed_id);
3182
                                        goto error;
3183
                                }
3184
                                gboolean paused = listener->paused;
3185
                                listener->paused = TRUE;
3186
                                /* Unsubscribe from the previous publisher */
3187
                                janus_videoroom_participant *prev_feed = listener->feed;
3188
                                if(prev_feed) {
3189
                                        janus_mutex_lock(&prev_feed->listeners_mutex);
3190
                                        prev_feed->listeners = g_slist_remove(prev_feed->listeners, listener);
3191
                                        janus_mutex_unlock(&prev_feed->listeners_mutex);
3192
                                        listener->feed = NULL;
3193
                                }
3194
                                /* Subscribe to the new one */
3195
                                listener->audio = audio ? json_is_true(audio) : TRUE;        /* True by default */
3196
                                if(!publisher->audio)
3197
                                        listener->audio = FALSE;        /* ... unless the publisher isn't sending any audio */
3198
                                listener->video = video ? json_is_true(video) : TRUE;        /* True by default */
3199
                                if(!publisher->video)
3200
                                        listener->video = FALSE;        /* ... unless the publisher isn't sending any video */
3201
                                listener->data = data ? json_is_true(data) : TRUE;        /* True by default */
3202
                                if(!publisher->data)
3203
                                        listener->data = FALSE;        /* ... unless the publisher isn't sending any data */
3204
                                janus_mutex_lock(&publisher->listeners_mutex);
3205
                                publisher->listeners = g_slist_append(publisher->listeners, listener);
3206
                                janus_mutex_unlock(&publisher->listeners_mutex);
3207
                                listener->feed = publisher;
3208
                                /* Send a FIR to the new publisher */
3209
                                char buf[20];
3210
                                memset(buf, 0, 20);
3211
                                janus_rtcp_fir((char *)&buf, 20, &publisher->fir_seq);
3212
                                JANUS_LOG(LOG_VERB, "Switching existing listener to new publisher, sending FIR to %"SCNu64" (%s)\n", publisher->user_id, publisher->display ? publisher->display : "??");
3213
                                gateway->relay_rtcp(publisher->session->handle, 1, buf, 20);
3214
                                /* Send a PLI too, just in case... */
3215
                                memset(buf, 0, 12);
3216
                                janus_rtcp_pli((char *)&buf, 12);
3217
                                JANUS_LOG(LOG_VERB, "Switching existing listener to new publisher, sending PLI to %"SCNu64" (%s)\n", publisher->user_id, publisher->display ? publisher->display : "??");
3218
                                gateway->relay_rtcp(publisher->session->handle, 1, buf, 12);
3219
                                /* Done */
3220
                                listener->paused = paused;
3221
                                event = json_object();
3222
                                json_object_set_new(event, "videoroom", json_string("event"));
3223
                                json_object_set_new(event, "switched", json_string("ok"));
3224
                                json_object_set_new(event, "room", json_integer(listener->room->room_id));
3225
                                json_object_set_new(event, "id", json_integer(feed_id));
3226
                                if(publisher->display)
3227
                                        json_object_set_new(event, "display", json_string(publisher->display));
3228
                                /* Also notify event handlers */
3229
                                if(notify_events && gateway->events_is_enabled()) {
3230
                                        json_t *info = json_object();
3231
                                        json_object_set_new(info, "event", json_string("switched"));
3232
                                        json_object_set_new(info, "room", json_integer(publisher->room->room_id));
3233
                                        json_object_set_new(info, "feed", json_integer(publisher->user_id));
3234
                                        gateway->notify_event(session->handle, info);
3235
                                }
3236
                        } else if(!strcasecmp(request_text, "leave")) {
3237
                                janus_videoroom_participant *publisher = listener->feed;
3238
                                if(publisher != NULL) {
3239
                                        janus_mutex_lock(&publisher->listeners_mutex);
3240
                                        publisher->listeners = g_slist_remove(publisher->listeners, listener);
3241
                                        janus_mutex_unlock(&publisher->listeners_mutex);
3242
                                        listener->feed = NULL;
3243
                                }
3244
                                event = json_object();
3245
                                json_object_set_new(event, "videoroom", json_string("event"));
3246
                                json_object_set_new(event, "room", json_integer(listener->room->room_id));
3247
                                json_object_set_new(event, "left", json_string("ok"));
3248
                                session->started = FALSE;
3249
                        } else {
3250
                                JANUS_LOG(LOG_ERR, "Unknown request '%s'\n", request_text);
3251
                                error_code = JANUS_VIDEOROOM_ERROR_INVALID_REQUEST;
3252
                                g_snprintf(error_cause, 512, "Unknown request '%s'", request_text);
3253
                                goto error;
3254
                        }
3255
                } else if(session->participant_type == janus_videoroom_p_type_subscriber_muxed) {
3256
                        /* Handle this Multiplexed listener */
3257
                        janus_videoroom_listener_muxed *listener = (janus_videoroom_listener_muxed *)session->participant;
3258
                        if(listener == NULL) {
3259
                                JANUS_LOG(LOG_ERR, "Invalid Multiplexed listener instance\n");
3260
                                error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;
3261
                                g_snprintf(error_cause, 512, "Invalid Multiplexed listener instance");
3262
                                goto error;
3263
                        }
3264
                        if(!strcasecmp(request_text, "join")) {
3265
                                JANUS_LOG(LOG_ERR, "Already in as a Multiplexed listener on this handle\n");
3266
                                error_code = JANUS_VIDEOROOM_ERROR_ALREADY_JOINED;
3267
                                g_snprintf(error_cause, 512, "Already in as a Multiplexed listener on this handle");
3268
                                goto error;
3269
                        } else if(!strcasecmp(request_text, "add")) {
3270
                                /* Add new streams to subscribe to */
3271
                                GList *list = NULL;
3272
                                JANUS_VALIDATE_JSON_OBJECT(root, feeds_parameters,
3273
                                        error_code, error_cause, TRUE,
3274
                                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
3275
                                if(error_code != 0)
3276
                                        goto error;
3277
                                json_t *feeds = json_object_get(root, "feeds");
3278
                                unsigned int i = 0;
3279
                                int problem = 0;
3280
                                if(!listener->room) {
3281
                                        JANUS_LOG(LOG_ERR, "Room Destroyed ");
3282
                                        error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_ROOM;
3283
                                        g_snprintf(error_cause, 512, "No such room ");
3284
                                        goto error;
3285
                                }
3286
                                if(listener->room->destroyed) {
3287
                                        JANUS_LOG(LOG_ERR, "Room Destroyed (%"SCNu64")", listener->room->room_id);
3288
                                        error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_ROOM;
3289
                                        g_snprintf(error_cause, 512, "No such room (%"SCNu64")", listener->room->room_id);
3290
                                        goto error;
3291
                                }
3292
                                for(i=0; i<json_array_size(feeds); i++) {
3293
                                        json_t *feed = json_array_get(feeds, i);
3294
                                        if(listener->room->destroyed) {
3295
                                                problem = 1;
3296
                                                JANUS_LOG(LOG_ERR, "Room destroyed");
3297
                                                error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_ROOM;
3298
                                                g_snprintf(error_cause, 512, "Room destroyed");
3299
                                                break;
3300
                                        }
3301
                                        if(!feed || !json_is_integer(feed)) {
3302
                                                problem = 1;
3303
                                                JANUS_LOG(LOG_ERR, "Invalid element (feeds in the array must be integers)\n");
3304
                                                error_code = JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT;
3305
                                                g_snprintf(error_cause, 512, "Invalid element (feeds in the array must be integers)");
3306
                                                break;
3307
                                        }
3308
                                        uint64_t feed_id = json_integer_value(feed);
3309
                                        janus_mutex_lock(&listener->room->participants_mutex);
3310
                                        janus_videoroom_participant *publisher = g_hash_table_lookup(listener->room->participants, &feed_id);
3311
                                        janus_mutex_unlock(&listener->room->participants_mutex);
3312
                                        if(publisher == NULL) { //~ || publisher->sdp == NULL) {
3313
                                                /* FIXME For muxed listeners, we accept subscriptions to existing participants who haven't published yet */
3314
                                                problem = 1;
3315
                                                JANUS_LOG(LOG_ERR, "No such feed (%"SCNu64")\n", feed_id);
3316
                                                error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_FEED;
3317
                                                g_snprintf(error_cause, 512, "No such feed (%"SCNu64")", feed_id);
3318
                                                break;
3319
                                        }
3320
                                        list = g_list_prepend(list, GUINT_TO_POINTER(feed_id));
3321
                                }
3322
                                if(problem) {
3323
                                        goto error;
3324
                                }
3325
                                list = g_list_reverse(list);
3326
                                if(janus_videoroom_muxed_subscribe(listener, list, msg->transaction) < 0) {
3327
                                        JANUS_LOG(LOG_ERR, "Error subscribing!\n");
3328
                                        error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;        /* FIXME */
3329
                                        g_snprintf(error_cause, 512, "Error subscribing!");
3330
                                        goto error;
3331
                                }
3332
                                janus_videoroom_message_free(msg);
3333
                                continue;
3334
                        } else if(!strcasecmp(request_text, "remove")) {
3335
                                /* Remove subscribed streams */
3336
                                GList *list = NULL;
3337
                                JANUS_VALIDATE_JSON_OBJECT(root, feeds_parameters,
3338
                                        error_code, error_cause, TRUE,
3339
                                        JANUS_VIDEOROOM_ERROR_MISSING_ELEMENT, JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT);
3340
                                if(error_code != 0)
3341
                                        goto error;
3342
                                json_t *feeds = json_object_get(root, "feeds");
3343
                                unsigned int i = 0;
3344
                                int error = 0;
3345
                                for(i=0; i<json_array_size(feeds); i++) {
3346
                                        json_t *feed = json_array_get(feeds, i);
3347
                                        if(!feed || !json_is_integer(feed)) {
3348
                                                error = 1;
3349
                                                break;
3350
                                        }
3351
                                        list = g_list_prepend(list, GUINT_TO_POINTER(json_integer_value(feed)));
3352
                                }
3353
                                if(error) {
3354
                                        JANUS_LOG(LOG_ERR, "Invalid element (feeds in the array must be integers)\n");
3355
                                        error_code = JANUS_VIDEOROOM_ERROR_INVALID_ELEMENT;
3356
                                        g_snprintf(error_cause, 512, "Invalid element (feeds in the array must be integers)");
3357
                                        goto error;
3358
                                }
3359
                                list = g_list_reverse(list);
3360
                                
3361
                                if(!listener->room) {
3362
                                        JANUS_LOG(LOG_ERR, "Error unsubscribing!\n");
3363
                                        error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;        /* FIXME */
3364
                                        g_snprintf(error_cause, 512, "Error unsubscribing!");
3365
                                        goto error;
3366
                                }
3367
                                if(janus_videoroom_muxed_unsubscribe(listener, list, msg->transaction) < 0) {
3368
                                        JANUS_LOG(LOG_ERR, "Error unsubscribing!\n");
3369
                                        error_code = JANUS_VIDEOROOM_ERROR_UNKNOWN_ERROR;        /* FIXME */
3370
                                        g_snprintf(error_cause, 512, "Error unsubscribing!");
3371
                                        goto error;
3372
                                }
3373
                                janus_videoroom_message_free(msg);
3374
                                continue;
3375
                        } else if(!strcasecmp(request_text, "start")) {
3376
                                /* Start/restart receiving the publishers streams */
3377
                                /* TODO */
3378
                                event = json_object();
3379
                                json_object_set_new(event, "videoroom", json_string("event"));
3380
                                json_object_set_new(event, "room", json_integer(listener->room->room_id));
3381
                                json_object_set_new(event, "started", json_string("ok"));
3382
                                //~ /* Send a FIR */
3383
                                //~ char buf[20];
3384
                                //~ memset(buf, 0, 20);
3385
                                //~ janus_rtcp_fir((char *)&buf, 20, &publisher->fir_seq);
3386
                                //~ JANUS_LOG(LOG_VERB, "Resuming publisher, sending FIR to %"SCNu64" (%s)\n", publisher->user_id, publisher->display ? publisher->display : "??");
3387
                                //~ gateway->relay_rtcp(publisher->session->handle, 1, buf, 20);
3388
                                //~ /* Send a PLI too, just in case... */
3389
                                //~ memset(buf, 0, 12);
3390
                                //~ janus_rtcp_pli((char *)&buf, 12);
3391
                                //~ JANUS_LOG(LOG_VERB, "Resuming publisher, sending PLI to %"SCNu64" (%s)\n", publisher->user_id, publisher->display ? publisher->display : "??");
3392
                                //~ gateway->relay_rtcp(publisher->session->handle, 1, buf, 12);
3393
                        } else if(!strcasecmp(request_text, "pause")) {
3394
                                /* Stop receiving the publishers streams for a while */
3395
                                /* TODO */
3396
                                event = json_object();
3397
                                json_object_set_new(event, "videoroom", json_string("event"));
3398
                                json_object_set_new(event, "room", json_integer(listener->room->room_id));
3399
                                json_object_set_new(event, "paused", json_string("ok"));
3400
                        } else if(!strcasecmp(request_text, "leave")) {
3401
                                /* TODO */
3402
                                event = json_object();
3403
                                json_object_set_new(event, "videoroom", json_string("event"));
3404
                                json_object_set_new(event, "room", json_integer(listener->room->room_id));
3405
                                json_object_set_new(event, "left", json_string("ok"));
3406
                                session->started = FALSE;
3407
                        } else {
3408
                                JANUS_LOG(LOG_ERR, "Unknown request '%s'\n", request_text);
3409
                                error_code = JANUS_VIDEOROOM_ERROR_INVALID_REQUEST;
3410
                                g_snprintf(error_cause, 512, "Unknown request '%s'", request_text);
3411
                                goto error;
3412
                        }
3413
                }
3414

    
3415
                /* Prepare JSON event */
3416
                JANUS_LOG(LOG_VERB, "Preparing JSON event as a reply\n");
3417
                char *event_text = json_dumps(event, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
3418
                json_decref(event);
3419
                /* Any SDP to handle? */
3420
                if(!msg->sdp) {
3421
                        int ret = gateway->push_event(msg->handle, &janus_videoroom_plugin, msg->transaction, event_text, NULL, NULL);
3422
                        JANUS_LOG(LOG_VERB, "  >> %d (%s)\n", ret, janus_get_api_error(ret));
3423
                } else {
3424
                        JANUS_LOG(LOG_VERB, "This is involving a negotiation (%s) as well:\n%s\n", msg->sdp_type, msg->sdp);
3425
                        const char *type = NULL;
3426
                        if(!strcasecmp(msg->sdp_type, "offer")) {
3427
                                /* We need to answer */
3428
                                type = "answer";
3429
                        } else if(!strcasecmp(msg->sdp_type, "answer")) {
3430
                                /* We got an answer (from a listener?), no need to negotiate */
3431
                                g_atomic_int_set(&session->hangingup, 0);
3432
                                int ret = gateway->push_event(msg->handle, &janus_videoroom_plugin, msg->transaction, event_text, NULL, NULL);
3433
                                JANUS_LOG(LOG_VERB, "  >> %d (%s)\n", ret, janus_get_api_error(ret));
3434
                                g_free(event_text);
3435
                                root = NULL;
3436
                                janus_videoroom_message_free(msg);
3437
                                continue;
3438
                        } else {
3439
                                /* TODO We don't support anything else right now... */
3440
                                JANUS_LOG(LOG_ERR, "Unknown SDP type '%s'\n", msg->sdp_type);
3441
                                error_code = JANUS_VIDEOROOM_ERROR_INVALID_SDP_TYPE;
3442
                                g_snprintf(error_cause, 512, "Unknown SDP type '%s'", msg->sdp_type);
3443
                                goto error;
3444
                        }
3445
                        if(session->participant_type != janus_videoroom_p_type_publisher) {
3446
                                /* We shouldn't be here, we always offer ourselves */
3447
                                JANUS_LOG(LOG_ERR, "Only publishers send offers\n");
3448
                                error_code = JANUS_VIDEOROOM_ERROR_INVALID_SDP_TYPE;
3449
                                g_snprintf(error_cause, 512, "Only publishers send offers");
3450
                                goto error;
3451
                        } else {
3452
                                /* This is a new publisher: is there room? */
3453
                                janus_videoroom_participant *participant = (janus_videoroom_participant *)session->participant;
3454
                                janus_videoroom *videoroom = participant->room;
3455
                                int count = 0;
3456
                                GHashTableIter iter;
3457
                                gpointer value;
3458
                                if(!videoroom) {
3459
                                        error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_ROOM;
3460
                                        goto error;
3461
                                }
3462
                                if(videoroom->destroyed) {
3463
                                        error_code = JANUS_VIDEOROOM_ERROR_NO_SUCH_ROOM;
3464
                                        goto error;
3465
                                }
3466
                                janus_mutex_lock(&videoroom->participants_mutex);
3467
                                g_hash_table_iter_init(&iter, videoroom->participants);
3468
                                while (!videoroom->destroyed && g_hash_table_iter_next(&iter, NULL, &value)) {
3469
                                        janus_videoroom_participant *p = value;
3470
                                        if(p != participant && p->sdp)
3471
                                                count++;
3472
                                }
3473
                                janus_mutex_unlock(&videoroom->participants_mutex);
3474
                                if(count == videoroom->max_publishers) {
3475
                                        participant->audio_active = FALSE;
3476
                                        participant->video_active = FALSE;
3477
                                        JANUS_LOG(LOG_ERR, "Maximum number of publishers (%d) already reached\n", videoroom->max_publishers);
3478
                                        error_code = JANUS_VIDEOROOM_ERROR_PUBLISHERS_FULL;
3479
                                        g_snprintf(error_cause, 512, "Maximum number of publishers (%d) already reached", videoroom->max_publishers);
3480
                                        goto error;
3481
                                }
3482
                                /* Now prepare the SDP to give back */
3483
                                if(strstr(msg->sdp, "Mozilla")) {
3484
                                        participant->firefox = TRUE;
3485
                                }
3486
                                /* Which media are available? */
3487
                                int audio = 0, video = 0, data = 0;
3488
                                const char *audio_mode = NULL, *video_mode = NULL;
3489
                                sdp_parser_t *parser = sdp_parse(sdphome, msg->sdp, strlen(msg->sdp), 0);
3490
                                sdp_session_t *parsed_sdp = sdp_session(parser);
3491
                                if(!parsed_sdp) {
3492
                                        /* Invalid SDP */
3493
                                        JANUS_LOG(LOG_ERR, "Error parsing SDP: %s\n", sdp_parsing_error(parser));
3494
                                        error_code = JANUS_VIDEOROOM_ERROR_PUBLISHERS_FULL;
3495
                                        g_snprintf(error_cause, 512, "Error parsing SDP: %s", sdp_parsing_error(parser));
3496
                                        sdp_parser_free(parser);
3497
                                        goto error;
3498
                                }
3499
                                sdp_media_t *m = parsed_sdp->sdp_media;
3500
                                while(m) {
3501
                                        if(m->m_type == sdp_media_audio && m->m_port > 0) {
3502
                                                audio++;
3503
                                                participant->audio = TRUE;
3504
                                                if(audio > 1) {
3505
                                                        m = m->m_next;
3506
                                                        continue;
3507
                                                }
3508
                                        } else if(m->m_type == sdp_media_video && m->m_port > 0) {
3509
                                                video++;
3510
                                                participant->video = TRUE;
3511
                                                if(video > 1) {
3512
                                                        m = m->m_next;
3513
                                                        continue;
3514
                                                }
3515
#ifdef HAVE_SCTP
3516
                                        } else if(m->m_type == sdp_media_application && m->m_port > 0) {
3517
                                                data++;
3518
                                                participant->data = TRUE;
3519
                                                if(data > 1) {
3520
                                                        m = m->m_next;
3521
                                                        continue;
3522
                                                }
3523
#endif
3524
                                        }
3525
                                        if(m->m_type != sdp_media_application) {
3526
                                                /* What is the direction? */
3527
                                                switch(m->m_mode) {
3528
                                                        case sdp_recvonly:
3529
                                                                /* If we're getting a 'recvonly' publisher, we're going to answer with 'inactive' */
3530
                                                        case sdp_inactive:
3531
                                                                if(m->m_type == sdp_media_audio) {
3532
                                                                        audio_mode = "inactive";
3533
                                                                } else {
3534
                                                                        video_mode = "inactive";
3535
                                                                }
3536
                                                                break;
3537
                                                        case sdp_sendonly:
3538
                                                                /* What we expect, turn this into 'recvonly' */
3539
                                                        case sdp_sendrecv:
3540
                                                        default:
3541
                                                                if(m->m_type == sdp_media_audio) {
3542
                                                                        audio_mode = "recvonly";
3543
                                                                } else {
3544
                                                                        video_mode = "recvonly";
3545
                                                                }
3546
                                                                break;
3547
                                                }
3548
                                        }
3549
                                        m = m->m_next;
3550
                                }
3551
                                sdp_parser_free(parser);
3552
                                JANUS_LOG(LOG_VERB, "The publisher %s going to send an audio stream\n", audio ? "is" : "is NOT");
3553
                                int opus_pt = 0, isac32_pt = 0, isac16_pt = 0, pcmu_pt = 0, pcma_pt = 0,
3554
                                        vp8_pt = 0, vp9_pt = 0, h264_pt = 0;
3555
                                if(audio) {
3556
                                        JANUS_LOG(LOG_VERB, "  -- Will answer with media direction '%s'\n", audio_mode);
3557
                                        opus_pt = janus_get_codec_pt(msg->sdp, "opus");
3558
                                        if(opus_pt > 0) {
3559
                                                JANUS_LOG(LOG_VERB, "  -- -- Opus payload type is %d\n", opus_pt);
3560
                                        }
3561
                                        isac32_pt = janus_get_codec_pt(msg->sdp, "isac32");
3562
                                        if(isac32_pt > 0) {
3563
                                                JANUS_LOG(LOG_VERB, "  -- -- ISAC 32K payload type is %d\n", isac32_pt);
3564
                                        }
3565
                                        isac16_pt = janus_get_codec_pt(msg->sdp, "isac16");
3566
                                        if(isac16_pt > 0) {
3567
                                                JANUS_LOG(LOG_VERB, "  -- -- ISAC 16K payload type is %d\n", isac16_pt);
3568
                                        }
3569
                                        pcmu_pt = janus_get_codec_pt(msg->sdp, "pcmu");
3570
                                        if(pcmu_pt > 0) {
3571
                                                JANUS_LOG(LOG_VERB, "  -- -- PCMU payload type is %d\n", pcmu_pt);
3572
                                        }
3573
                                        pcma_pt = janus_get_codec_pt(msg->sdp, "pcma");
3574
                                        if(pcma_pt > 0) {
3575
                                                JANUS_LOG(LOG_VERB, "  -- -- PCMA payload type is %d\n", pcma_pt);
3576
                                        }
3577
                                }
3578
                                JANUS_LOG(LOG_VERB, "The publisher %s going to send a video stream\n", video ? "is" : "is NOT");
3579
                                if(video) {
3580
                                        JANUS_LOG(LOG_VERB, "  -- Will answer with media direction '%s'\n", video_mode);
3581
                                        vp8_pt = janus_get_codec_pt(msg->sdp, "vp8");
3582
                                        if(vp8_pt > 0) {
3583
                                                JANUS_LOG(LOG_VERB, "  -- -- VP8 payload type is %d\n", vp8_pt);
3584
                                        }
3585
                                        vp9_pt = janus_get_codec_pt(msg->sdp, "vp9");
3586
                                        if(vp9_pt > 0) {
3587
                                                JANUS_LOG(LOG_VERB, "  -- -- VP9 payload type is %d\n", vp9_pt);
3588
                                        }
3589
                                        h264_pt = janus_get_codec_pt(msg->sdp, "h264");
3590
                                        if(h264_pt > 0) {
3591
                                                JANUS_LOG(LOG_VERB, "  -- -- H264 payload type is %d\n", h264_pt);
3592
                                        }
3593
                                }
3594
                                JANUS_LOG(LOG_VERB, "The publisher %s going to open a data channel\n", data ? "is" : "is NOT");
3595
                                /* Also add a bandwidth SDP attribute if we're capping the bitrate in the room */
3596
                                int b = 0;
3597
                                if(participant->firefox)        /* Don't add any b=AS attribute for Chrome */
3598
                                        b = (int)(videoroom->bitrate/1000);
3599
                                char sdp[1280], audio_mline[256], video_mline[512], data_mline[256];
3600
                                char *newsdp = NULL;
3601
                                int res = 0;
3602
                                int pass = 0;
3603
                                for(pass = 1; pass <= 2; pass++) {
3604
                                        if(pass == 2) {
3605
                                                /* Now turn the SDP into what we'll send subscribers, using the static payload types for making switching easier */
3606
                                                if(audio_mode && strcmp(audio_mode, "inactive"))
3607
                                                        /* The publisher gets a recvonly or inactive back */
3608
                                                        /* Subscribers gets a sendonly or inactive back */
3609
                                                        audio_mode = "sendonly";
3610
                                                if(video_mode && strcmp(video_mode, "inactive"))
3611
                                                        video_mode = "sendonly";
3612
                                        }
3613
                                        audio_mline[0] = '\0';
3614
                                        if(audio) {
3615
                                                int pt = -1;
3616
                                                switch(videoroom->acodec) {
3617
                                                        case JANUS_VIDEOROOM_OPUS:
3618
                                                                if(opus_pt >= 0)
3619
                                                                        pt = (pass == 1 ? opus_pt : OPUS_PT);
3620
                                                                break;
3621
                                                        case JANUS_VIDEOROOM_ISAC_32K:
3622
                                                                if(isac32_pt >= 0)
3623
                                                                        pt = (pass == 1 ? isac32_pt : ISAC32_PT);
3624
                                                                break;
3625
                                                        case JANUS_VIDEOROOM_ISAC_16K:
3626
                                                                if(isac16_pt >= 0)
3627
                                                                        pt = (pass == 1 ? isac16_pt : ISAC16_PT);
3628
                                                                break;
3629
                                                        case JANUS_VIDEOROOM_PCMU:
3630
                                                                if(pcmu_pt >= 0)
3631
                                                                        pt = (pass == 1 ? pcmu_pt : PCMU_PT);
3632
                                                                break;
3633
                                                        case JANUS_VIDEOROOM_PCMA:
3634
                                                                if(pcma_pt >= 0)
3635
                                                                        pt = (pass == 1 ? pcma_pt : PCMA_PT);
3636
                                                                break;
3637
                                                        default:
3638
                                                                /* Shouldn't happen */
3639
                                                                break;
3640
                                                }
3641
                                                if(pass == 1 && pt < 0)
3642
                                                        JANUS_LOG(LOG_WARN, "Videoroom is forcing %s, but publisher didn't offer any... rejecting audio\n", janus_videoroom_audiocodec_name(videoroom->acodec));
3643
                                                if(pt >= 0)
3644
                                                        janus_videoroom_sdp_a_format(audio_mline, 256, videoroom->acodec, pt, audio_mode);
3645
                                                if(audio_mline[0] == '\0' && pass == 1) {
3646
                                                        /* Remove "pass == 1" if the listener also should get a line with port=0. */
3647
                                                        g_snprintf(audio_mline, 256, "m=audio 0 RTP/SAVPF 0\r\n");
3648
                                                }
3649
                                        }
3650
                                        video_mline[0] = '\0';
3651
                                        if(video) {
3652
                                                int pt = -1;
3653
                                                switch(videoroom->vcodec) {
3654
                                                        case JANUS_VIDEOROOM_VP8:
3655
                                                                if(vp8_pt >= 0)
3656
                                                                        pt = (pass == 1 ? vp8_pt : VP8_PT);
3657
                                                                break;
3658
                                                        case JANUS_VIDEOROOM_VP9:
3659
                                                                if(vp9_pt >= 0)
3660
                                                                        pt = (pass == 1 ? vp9_pt : VP9_PT);
3661
                                                                break;
3662
                                                        case JANUS_VIDEOROOM_H264:
3663
                                                                if(h264_pt >= 0)
3664
                                                                        pt = (pass == 1 ? h264_pt : H264_PT);
3665
                                                                break;
3666
                                                        default:
3667
                                                                /* Shouldn't happen */
3668
                                                                break;
3669
                                                }
3670
                                                if(pass == 1 && pt < 0)
3671
                                                        JANUS_LOG(LOG_WARN, "Videoroom is forcing %s, but publisher didn't offer any... rejecting video\n", janus_videoroom_videocodec_name(videoroom->vcodec));
3672
                                                if(pt >= 0)
3673
                                                        janus_videoroom_sdp_v_format(video_mline, 512, videoroom->vcodec, pt, b, video_mode);
3674
                                                if(video_mline[0] == '\0' && pass == 1) {
3675
                                                        /* Remove "pass == 1" if the listener also should get a line with port=0. */
3676
                                                        g_snprintf(video_mline, 512, "m=video 0 RTP/SAVPF 0\r\n");
3677
                                                }
3678
                                        }
3679
                                        if(data) {
3680
                                                g_snprintf(data_mline, 256, sdp_d_template);
3681
                                        } else {
3682
                                                data_mline[0] = '\0';
3683
                                        }
3684
                                        g_snprintf(sdp, 1280, sdp_template,
3685
                                                janus_get_real_time(),                        /* We need current time here */
3686
                                                janus_get_real_time(),                        /* We need current time here */
3687
                                                participant->room->room_name,        /* Video room name */
3688
                                                audio_mline,                                        /* Audio m-line, if any */
3689
                                                video_mline,                                        /* Video m-line, if any */
3690
                                                data_mline);                                        /* Data channel m-line, if any */
3691
                                        newsdp = g_strdup(sdp);
3692
                                        if(video && b == 0) {
3693
                                                /* Remove useless bandwidth attribute */
3694
                                                newsdp = janus_string_replace(newsdp, "b=AS:0\r\n", "");
3695
                                        }
3696
                                        if(pass == 2)
3697
                                                break;
3698
                                        /* Is this room recorded? */
3699
                                        janus_mutex_lock(&participant->rec_mutex);
3700
                                        if(videoroom->record || participant->recording_active) {
3701
                                                janus_videoroom_recorder_create(participant, audio, video);
3702
                                        }
3703
                                        janus_mutex_unlock(&participant->rec_mutex);
3704

    
3705
                                        JANUS_LOG(LOG_VERB, "Handling publisher: turned this into an '%s':\n%s\n", type, newsdp);
3706
                                        /* How long will the gateway take to push the event? */
3707
                                        g_atomic_int_set(&session->hangingup, 0);
3708
                                        gint64 start = janus_get_monotonic_time();
3709
                                        res = gateway->push_event(msg->handle, &janus_videoroom_plugin, msg->transaction, event_text, type, newsdp);
3710
                                        JANUS_LOG(LOG_VERB, "  >> Pushing event: %d (took %"SCNu64" us)\n", res, janus_get_monotonic_time()-start);
3711
                                        g_free(newsdp);
3712
                                }
3713
                                /* Done */
3714
                                if(res != JANUS_OK) {
3715
                                        /* TODO Failed to negotiate? We should remove this publisher */
3716
                                        g_free(newsdp);
3717
                                } else {
3718
                                        /* Store the participant's SDP for interested listeners */
3719
                                        participant->sdp = newsdp;
3720
                                        /* We'll wait for the setup_media event before actually telling listeners */
3721
                                }
3722
                        }
3723
                }
3724
                g_free(event_text);
3725
                janus_videoroom_message_free(msg);
3726

    
3727
                continue;
3728
                
3729
error:
3730
                {
3731
                        /* Prepare JSON error event */
3732
                        json_t *event = json_object();
3733
                        json_object_set_new(event, "videoroom", json_string("event"));
3734
                        json_object_set_new(event, "error_code", json_integer(error_code));
3735
                        json_object_set_new(event, "error", json_string(error_cause));
3736
                        char *event_text = json_dumps(event, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
3737
                        json_decref(event);
3738
                        JANUS_LOG(LOG_VERB, "Pushing event: %s\n", event_text);
3739
                        int ret = gateway->push_event(msg->handle, &janus_videoroom_plugin, msg->transaction, event_text, NULL, NULL);
3740
                        JANUS_LOG(LOG_VERB, "  >> %d (%s)\n", ret, janus_get_api_error(ret));
3741
                        g_free(event_text);
3742
                        janus_videoroom_message_free(msg);
3743
                }
3744
        }
3745
        JANUS_LOG(LOG_VERB, "Leaving VideoRoom handler thread\n");
3746
        return NULL;
3747
}
3748

    
3749

    
3750
/* Multiplexing helpers */
3751
int janus_videoroom_muxed_subscribe(janus_videoroom_listener_muxed *muxed_listener, GList *feeds, char *transaction) {
3752
        if(!muxed_listener || !feeds)
3753
                return -1;
3754
        janus_mutex_lock(&muxed_listener->listeners_mutex);
3755
        JANUS_LOG(LOG_VERB, "Subscribing to %d feeds\n", g_list_length(feeds));
3756
        janus_videoroom *videoroom = muxed_listener->room;
3757
        GList *ps = feeds;
3758
        json_t *list = json_array();
3759
        int added_feeds = 0;
3760
        while(ps) {
3761
                uint64_t feed_id = GPOINTER_TO_UINT(ps->data);
3762
                janus_videoroom_participant *publisher = g_hash_table_lookup(videoroom->participants, &feed_id);
3763
                if(publisher == NULL) { //~ || publisher->sdp == NULL) {
3764
                        /* FIXME For muxed listeners, we accept subscriptions to existing participants who haven't published yet */
3765
                        JANUS_LOG(LOG_WARN, "No such feed (%"SCNu64"), skipping\n", feed_id);
3766
                        ps = ps->next;
3767
                        continue;
3768
                }
3769
                /* Are we already subscribed? */
3770
                gboolean subscribed = FALSE;
3771
                GSList *ls = muxed_listener->listeners;
3772
                while(ls) {
3773
                        janus_videoroom_listener *l = (janus_videoroom_listener *)ls->data;
3774
                        if(l && (l->feed == publisher)) {
3775
                                subscribed = TRUE;
3776
                                JANUS_LOG(LOG_WARN, "Already subscribed to feed %"SCNu64", skipping\n", feed_id);
3777
                                break;
3778
                        }
3779
                        ls = ls->next;
3780
                }
3781
                if(subscribed) {
3782
                        ps = ps->next;
3783
                        continue;
3784
                }
3785
                janus_videoroom_listener *listener = g_malloc0(sizeof(janus_videoroom_listener));
3786
                if(listener == NULL) {
3787
                        JANUS_LOG(LOG_FATAL, "Memory error!\n");
3788
                        ps = ps->next;
3789
                        continue;
3790
                }
3791
                listener->session = muxed_listener->session;
3792
                listener->room = videoroom;
3793
                listener->feed = publisher;
3794
                //~ listener->paused = TRUE;        /* We need an explicit start from the listener */
3795
                listener->paused = FALSE;
3796
                listener->parent = muxed_listener;
3797
                janus_mutex_lock(&publisher->listeners_mutex);
3798
                publisher->listeners = g_slist_append(publisher->listeners, listener);
3799
                janus_mutex_unlock(&publisher->listeners_mutex);
3800
                muxed_listener->listeners = g_slist_append(muxed_listener->listeners, listener);
3801
                JANUS_LOG(LOG_VERB, "Now subscribed to %d feeds\n", g_slist_length(muxed_listener->listeners));
3802
                /* Add to feeds in the answer */
3803
                added_feeds++;
3804
                json_t *f = json_object();
3805
                json_object_set_new(f, "id", json_integer(feed_id));
3806
                if(publisher->display)
3807
                        json_object_set_new(f, "display", json_string(publisher->display));
3808
                json_array_append_new(list, f);
3809
                ps = ps->next;
3810
        }
3811
        janus_mutex_unlock(&muxed_listener->listeners_mutex);
3812
        if(added_feeds == 0) {
3813
                /* Nothing changed */
3814
                return 0;
3815
        }
3816
        /* Prepare event */
3817
        json_t *event = json_object();
3818
        json_object_set_new(event, "videoroom", json_string("muxed-attached"));
3819
        json_object_set_new(event, "room", json_integer(videoroom->room_id));
3820
        json_object_set_new(event, "feeds", list);
3821
        JANUS_LOG(LOG_VERB, "Preparing JSON event as a reply\n");
3822
        char *event_text = json_dumps(event, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
3823
        json_decref(event);
3824
        /* Send the updated offer */
3825
        return janus_videoroom_muxed_offer(muxed_listener, transaction, event_text);
3826
}
3827

    
3828
int janus_videoroom_muxed_unsubscribe(janus_videoroom_listener_muxed *muxed_listener, GList *feeds, char *transaction) {
3829
        janus_mutex_lock(&muxed_listener->listeners_mutex);
3830
        JANUS_LOG(LOG_VERB, "Unsubscribing from %d feeds\n", g_list_length(feeds));
3831
        janus_videoroom *videoroom = muxed_listener->room;
3832
        GList *ps = feeds;
3833
        json_t *list = json_array();
3834
        int removed_feeds = 0;
3835
        while(ps) {
3836
                uint64_t feed_id = GPOINTER_TO_UINT(ps->data);
3837
                GSList *ls = muxed_listener->listeners;
3838
                while(ls) {
3839
                        janus_videoroom_listener *listener = (janus_videoroom_listener *)ls->data;
3840
                        if(listener) {
3841
                                janus_videoroom_participant *publisher = listener->feed;
3842
                                if(publisher == NULL || publisher->user_id != feed_id) {
3843
                                        /* Not the publisher we're looking for */
3844
                                        ls = ls->next;
3845
                                        continue;
3846
                                }
3847
                                janus_mutex_lock(&publisher->listeners_mutex);
3848
                                publisher->listeners = g_slist_remove(publisher->listeners, listener);
3849
                                janus_mutex_unlock(&publisher->listeners_mutex);
3850
                                listener->feed = NULL;
3851
                                muxed_listener->listeners = g_slist_remove(muxed_listener->listeners, listener);
3852
                                JANUS_LOG(LOG_VERB, "Now subscribed to %d feeds\n", g_slist_length(muxed_listener->listeners));
3853
                                janus_videoroom_listener_free(listener);
3854
                                /* Add to feeds in the answer */
3855
                                removed_feeds++;
3856
                                json_t *f = json_object();
3857
                                json_object_set_new(f, "id", json_integer(feed_id));
3858
                                json_array_append_new(list, f);
3859
                                break;
3860
                        }
3861
                        ls = ls->next;
3862
                }
3863
                ps = ps->next;
3864
        }
3865
        janus_mutex_unlock(&muxed_listener->listeners_mutex);
3866
        if(removed_feeds == 0) {
3867
                /* Nothing changed */
3868
                return 0;
3869
        }
3870
        /* Prepare event */
3871
        json_t *event = json_object();
3872
        json_object_set_new(event, "videoroom", json_string("muxed-detached"));
3873
        json_object_set_new(event, "room", json_integer(videoroom->room_id));
3874
        json_object_set_new(event, "feeds", list);
3875 <