Statistics
| Branch: | Revision:

janus-gateway / plugins / janus_videocall.c @ 78955474

History | View | Annotate | Download (42.5 KB)

1
/*! \file   janus_videocall.c
2
 * \author Lorenzo Miniero <lorenzo@meetecho.com>
3
 * \copyright GNU General Public License v3
4
 * \brief  Janus VideoCall plugin
5
 * \details  This is a simple video call plugin for Janus, allowing two
6
 * WebRTC peers to call each other through the gateway. The idea is to
7
 * provide a similar service as the well known AppRTC demo (https://apprtc.appspot.com),
8
 * but with the media flowing through the gateway rather than being peer-to-peer.
9
 * 
10
 * The plugin provides a simple fake registration mechanism. A peer attaching
11
 * to the plugin needs to specify a username, which acts as a "phone number":
12
 * if the username is free, it is associated with the peer, which means
13
 * he/she can be "called" using that username by another peer. Peers can
14
 * either "call" another peer, by specifying their username, or wait for a call.
15
 * The approach used by this plugin is similar to the one employed by the
16
 * echo test one: all frames (RTP/RTCP) coming from one peer are relayed
17
 * to the other.
18
 * 
19
 * Just as in the janus_videocall.c plugin, there are knobs to control
20
 * whether audio and/or video should be muted or not, and if the bitrate
21
 * of the peer needs to be capped by means of REMB messages.
22
 * 
23
 * \section vcallapi Video Call API
24
 * 
25
 * All requests you can send in the Video Call API are asynchronous,
26
 * which means all responses (successes and errors) will be delivered
27
 * as events with the same transaction. 
28
 * 
29
 * The supported requests are \c list , \c register , \c call ,
30
 * \c accept , \c set and \c hangup . \c list allows you to get a list
31
 * of all the registered peers; \c register can be used to register
32
 * a username to call and be called; \c call is used to start a video
33
 * call with somebody through the plugin, while \c accept is used to
34
 * accept the call in case one is invited instead of inviting; \c set
35
 * can be used to configure some call-related settings (e.g., a cap on
36
 * the send bandwidth); finally, \c hangup can be used to terminate the
37
 * communication at any time, either to hangup an ongoing call or to
38
 * cancel/decline a call that hasn't started yet.
39
 * 
40
 * The \c list request has to be formatted as follows:
41
 * 
42
\verbatim
43
{
44
        "request" : "list"
45
}
46
\endverbatim
47
 *
48
 * A successful request will result in an array of peers to be returned:
49
 * 
50
\verbatim
51
{
52
        "videocall" : "event",
53
        "result" : {
54
                "list": [        // Array of peers
55
                        "alice78",
56
                        "bob51",
57
                        // others
58
                ]
59
        }
60
}
61
\endverbatim
62
 * 
63
 * An error instead (and the same applies to all other requests, so this
64
 * won't be repeated) would provide both an error code and a more verbose
65
 * description of the cause of the issue:
66
 * 
67
\verbatim
68
{
69
        "videocall" : "event",
70
        "error_code" : <numeric ID, check Macros below>,
71
        "error" : "<error description as a string>"
72
}
73
\endverbatim
74
 * 
75
 * To register a username to call and be called, the \c register request
76
 * can be used. This works on a "first come, first served" basis: there's
77
 * no authetication involved, you just specify the username you'd like
78
 * to use and, if free, it's assigned to you. The \c request has to be
79
 * formatted as follows:
80
 * 
81
\verbatim
82
{
83
        "request" : "register",
84
        "username" : "<desired unique username>"
85
}
86
\endverbatim
87
 * 
88
 * If successul, this will result in a \c registered event:
89
 * 
90
\verbatim
91
{
92
        "videocall" : "event",
93
        "result" : {
94
                "event" : "registered",
95
                "username" : "<same username, registered>"
96
        }
97
}
98
\endverbatim
99
 * 
100
 * Once you're registered, you can either start a new call or wait to
101
 * be called by someone else who knows your username. To start a new
102
 * call, the \c call request can be used: this request must be attached
103
 * to a JSEP offer containing the WebRTC-related info to setup a new
104
 * media session. A \c call request has to be formatted as follows:
105
 * 
106
\verbatim
107
{
108
        "request" : "call",
109
        "username" : "<username to call>"
110
}
111
\endverbatim
112
 * 
113
 * If successul, this will result in a \c calling event:
114
 * 
115
\verbatim
116
{
117
        "videocall" : "event",
118
        "result" : {
119
                "event" : "calling",
120
                "username" : "<same username, registered>"
121
        }
122
}
123
\endverbatim
124
 *
125
 * At the same time, the user being called will receive an
126
 * \c incomingcall event
127
 *  
128
\verbatim
129
{
130
        "videocall" : "event",
131
        "result" : {
132
                "event" : "incomingcall",
133
                "username" : "<your username>"
134
        }
135
}
136
\endverbatim
137
 * 
138
 * To accept the call, the \c accept request can be used. This request
139
 * must be attached to a JSEP answer containing the WebRTC-related
140
 * information to complete the actual PeerConnection setup. A \c accept
141
 * request has to be formatted as follows:
142
 * 
143
\verbatim
144
{
145
        "request" : "accept"
146
}
147
\endverbatim
148
 * 
149
 * If successul, both the caller and the callee will receive an
150
 * \c accepted event to notify them about the success of the signalling:
151
 * 
152
\verbatim
153
{
154
        "videocall" : "event",
155
        "result" : {
156
                "event" : "accepted",
157
                "username" : "<caller username>"
158
        }
159
}
160
\endverbatim
161
 *
162
 * At this point, the media-related settings of the call can be modified
163
 * on either side by means of a \c set request, which acts pretty much
164
 * as the one in the \ref echoapi . The \c set request has to be
165
 * formatted as follows:
166
 *
167
\verbatim
168
{
169
        "request" : "set",
170
        "audio" : true|false,
171
        "video" : true|false,
172
        "bitrate" : <numeric bitrate value>
173
}
174
\endverbatim
175
 *
176
 * \c audio instructs the plugin to do or do not relay audio frames;
177
 * \c video does the same for video; \c bitrate caps the bandwidth to
178
 * force on the browser encoding side (e.g., 128000 for 128kbps).
179
 * A successful request will result in a \c set event:
180
 * 
181
\verbatim
182
{
183
        "videocall" : "event",
184
        "result" : {
185
                "event" : "set"
186
        }
187
}
188
\endverbatim
189
 * 
190
 * To decline an incoming call, cancel an attempt to call or simply
191
 * hangup an ongoing conversation, the \c hangup request can be used,
192
 * which has to be formatted as follows:
193
 * 
194
\verbatim
195
{
196
        "request" : "hangup"
197
}
198
\endverbatim
199
 *
200
 * Whatever the reason of a call being closed (e.g., a \c hangup request,
201
 * a PeerConnection being closed, or something else), both parties in
202
 * the communication will receive a \c hangup event:
203
 * 
204
\verbatim
205
{
206
        "videocall" : "event",
207
        "result" : {
208
                "event" : "hangup",
209
                "username" : "<username of who closed the communication>",
210
                "reason" : "<description of what happened>"
211
        }
212
}
213
\endverbatim
214
 * 
215
 * \ingroup plugins
216
 * \ref plugins
217
 */
218

    
219
#include "plugin.h"
220

    
221
#include <jansson.h>
222

    
223
#include "../debug.h"
224
#include "../apierror.h"
225
#include "../config.h"
226
#include "../mutex.h"
227
#include "../rtcp.h"
228
#include "../utils.h"
229

    
230

    
231
/* Plugin information */
232
#define JANUS_VIDEOCALL_VERSION                        5
233
#define JANUS_VIDEOCALL_VERSION_STRING        "0.0.5"
234
#define JANUS_VIDEOCALL_DESCRIPTION                "This is a simple video call plugin for Janus, allowing two WebRTC peers to call each other through the gateway."
235
#define JANUS_VIDEOCALL_NAME                        "JANUS VideoCall plugin"
236
#define JANUS_VIDEOCALL_AUTHOR                        "Meetecho s.r.l."
237
#define JANUS_VIDEOCALL_PACKAGE                        "janus.plugin.videocall"
238

    
239
/* Plugin methods */
240
janus_plugin *create(void);
241
int janus_videocall_init(janus_callbacks *callback, const char *config_path);
242
void janus_videocall_destroy(void);
243
int janus_videocall_get_api_compatibility(void);
244
int janus_videocall_get_version(void);
245
const char *janus_videocall_get_version_string(void);
246
const char *janus_videocall_get_description(void);
247
const char *janus_videocall_get_name(void);
248
const char *janus_videocall_get_author(void);
249
const char *janus_videocall_get_package(void);
250
void janus_videocall_create_session(janus_plugin_session *handle, int *error);
251
struct janus_plugin_result *janus_videocall_handle_message(janus_plugin_session *handle, char *transaction, char *message, char *sdp_type, char *sdp);
252
void janus_videocall_setup_media(janus_plugin_session *handle);
253
void janus_videocall_incoming_rtp(janus_plugin_session *handle, int video, char *buf, int len);
254
void janus_videocall_incoming_rtcp(janus_plugin_session *handle, int video, char *buf, int len);
255
void janus_videocall_incoming_data(janus_plugin_session *handle, char *buf, int len);
256
void janus_videocall_slow_link(janus_plugin_session *handle, int uplink, int video);
257
void janus_videocall_hangup_media(janus_plugin_session *handle);
258
void janus_videocall_destroy_session(janus_plugin_session *handle, int *error);
259
char *janus_videocall_query_session(janus_plugin_session *handle);
260

    
261
/* Plugin setup */
262
static janus_plugin janus_videocall_plugin =
263
        JANUS_PLUGIN_INIT (
264
                .init = janus_videocall_init,
265
                .destroy = janus_videocall_destroy,
266

    
267
                .get_api_compatibility = janus_videocall_get_api_compatibility,
268
                .get_version = janus_videocall_get_version,
269
                .get_version_string = janus_videocall_get_version_string,
270
                .get_description = janus_videocall_get_description,
271
                .get_name = janus_videocall_get_name,
272
                .get_author = janus_videocall_get_author,
273
                .get_package = janus_videocall_get_package,
274
                
275
                .create_session = janus_videocall_create_session,
276
                .handle_message = janus_videocall_handle_message,
277
                .setup_media = janus_videocall_setup_media,
278
                .incoming_rtp = janus_videocall_incoming_rtp,
279
                .incoming_rtcp = janus_videocall_incoming_rtcp,
280
                .incoming_data = janus_videocall_incoming_data,
281
                .slow_link = janus_videocall_slow_link,
282
                .hangup_media = janus_videocall_hangup_media,
283
                .destroy_session = janus_videocall_destroy_session,
284
                .query_session = janus_videocall_query_session,
285
        );
286

    
287
/* Plugin creator */
288
janus_plugin *create(void) {
289
        JANUS_LOG(LOG_VERB, "%s created!\n", JANUS_VIDEOCALL_NAME);
290
        return &janus_videocall_plugin;
291
}
292

    
293

    
294
/* Useful stuff */
295
static gint initialized = 0, stopping = 0;
296
static janus_callbacks *gateway = NULL;
297
static GThread *handler_thread;
298
static GThread *watchdog;
299
static void *janus_videocall_handler(void *data);
300

    
301
typedef struct janus_videocall_message {
302
        janus_plugin_session *handle;
303
        char *transaction;
304
        char *message;
305
        char *sdp_type;
306
        char *sdp;
307
} janus_videocall_message;
308
static GAsyncQueue *messages = NULL;
309

    
310
void janus_videocall_message_free(janus_videocall_message *msg);
311
void janus_videocall_message_free(janus_videocall_message *msg) {
312
        if(!msg)
313
                return;
314

    
315
        msg->handle = NULL;
316

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

    
326
        g_free(msg);
327
}
328

    
329
typedef struct janus_videocall_session {
330
        janus_plugin_session *handle;
331
        gchar *username;
332
        gboolean audio_active;
333
        gboolean video_active;
334
        uint64_t bitrate;
335
        guint16 slowlink_count;
336
        struct janus_videocall_session *peer;
337
        gboolean hangingup;
338
        gint64 destroyed;        /* Time at which this session was marked as destroyed */
339
} janus_videocall_session;
340
static GHashTable *sessions;
341
static GList *old_sessions;
342
static janus_mutex sessions_mutex;
343

    
344

    
345
/* Error codes */
346
#define JANUS_VIDEOCALL_ERROR_UNKNOWN_ERROR                        499
347
#define JANUS_VIDEOCALL_ERROR_NO_MESSAGE                        470
348
#define JANUS_VIDEOCALL_ERROR_INVALID_JSON                        471
349
#define JANUS_VIDEOCALL_ERROR_INVALID_REQUEST                472
350
#define JANUS_VIDEOCALL_ERROR_REGISTER_FIRST                473
351
#define JANUS_VIDEOCALL_ERROR_INVALID_ELEMENT                474
352
#define JANUS_VIDEOCALL_ERROR_MISSING_ELEMENT                475
353
#define JANUS_VIDEOCALL_ERROR_USERNAME_TAKEN                476
354
#define JANUS_VIDEOCALL_ERROR_ALREADY_REGISTERED        477
355
#define JANUS_VIDEOCALL_ERROR_NO_SUCH_USERNAME                478
356
#define JANUS_VIDEOCALL_ERROR_USE_ECHO_TEST                        479
357
#define JANUS_VIDEOCALL_ERROR_ALREADY_IN_CALL                480
358
#define JANUS_VIDEOCALL_ERROR_NO_CALL                                481
359
#define JANUS_VIDEOCALL_ERROR_MISSING_SDP                        482
360

    
361

    
362
/* VideoCall watchdog/garbage collector (sort of) */
363
void *janus_videocall_watchdog(void *data);
364
void *janus_videocall_watchdog(void *data) {
365
        JANUS_LOG(LOG_INFO, "VideoCall watchdog started\n");
366
        gint64 now = 0;
367
        while(g_atomic_int_get(&initialized) && !g_atomic_int_get(&stopping)) {
368
                janus_mutex_lock(&sessions_mutex);
369
                /* Iterate on all the sessions */
370
                now = janus_get_monotonic_time();
371
                if(old_sessions != NULL) {
372
                        GList *sl = old_sessions;
373
                        JANUS_LOG(LOG_HUGE, "Checking %d old VideoCall sessions...\n", g_list_length(old_sessions));
374
                        while(sl) {
375
                                janus_videocall_session *session = (janus_videocall_session *)sl->data;
376
                                if(!session) {
377
                                        sl = sl->next;
378
                                        continue;
379
                                }
380
                                if(now-session->destroyed >= 5*G_USEC_PER_SEC) {
381
                                        /* We're lazy and actually get rid of the stuff only after a few seconds */
382
                                        JANUS_LOG(LOG_VERB, "Freeing old VideoCall session\n");
383
                                        GList *rm = sl->next;
384
                                        old_sessions = g_list_delete_link(old_sessions, sl);
385
                                        sl = rm;
386
                                        session->handle = NULL;
387
                                        g_free(session);
388
                                        session = NULL;
389
                                        continue;
390
                                }
391
                                sl = sl->next;
392
                        }
393
                }
394
                janus_mutex_unlock(&sessions_mutex);
395
                g_usleep(500000);
396
        }
397
        JANUS_LOG(LOG_INFO, "VideoCall watchdog stopped\n");
398
        return NULL;
399
}
400

    
401

    
402
/* Plugin implementation */
403
int janus_videocall_init(janus_callbacks *callback, const char *config_path) {
404
        if(g_atomic_int_get(&stopping)) {
405
                /* Still stopping from before */
406
                return -1;
407
        }
408
        if(callback == NULL || config_path == NULL) {
409
                /* Invalid arguments */
410
                return -1;
411
        }
412

    
413
        /* Read configuration */
414
        char filename[255];
415
        g_snprintf(filename, 255, "%s/%s.cfg", config_path, JANUS_VIDEOCALL_PACKAGE);
416
        JANUS_LOG(LOG_VERB, "Configuration file: %s\n", filename);
417
        janus_config *config = janus_config_parse(filename);
418
        if(config != NULL)
419
                janus_config_print(config);
420
        /* This plugin actually has nothing to configure... */
421
        janus_config_destroy(config);
422
        config = NULL;
423
        
424
        sessions = g_hash_table_new(g_str_hash, g_str_equal);
425
        janus_mutex_init(&sessions_mutex);
426
        messages = g_async_queue_new_full((GDestroyNotify) janus_videocall_message_free);
427
        /* This is the callback we'll need to invoke to contact the gateway */
428
        gateway = callback;
429

    
430
        g_atomic_int_set(&initialized, 1);
431

    
432
        GError *error = NULL;
433
        /* Start the sessions watchdog */
434
        watchdog = g_thread_try_new("vcall watchdog", &janus_videocall_watchdog, NULL, &error);
435
        if(error != NULL) {
436
                g_atomic_int_set(&initialized, 0);
437
                JANUS_LOG(LOG_ERR, "Got error %d (%s) trying to launch the VideoCall watchdog thread...\n", error->code, error->message ? error->message : "??");
438
                return -1;
439
        }
440
        /* Launch the thread that will handle incoming messages */
441
        handler_thread = g_thread_try_new("janus videocall handler", janus_videocall_handler, NULL, &error);
442
        if(error != NULL) {
443
                g_atomic_int_set(&initialized, 0);
444
                JANUS_LOG(LOG_ERR, "Got error %d (%s) trying to launch the VideoCall handler thread...\n", error->code, error->message ? error->message : "??");
445
                return -1;
446
        }
447
        JANUS_LOG(LOG_INFO, "%s initialized!\n", JANUS_VIDEOCALL_NAME);
448
        return 0;
449
}
450

    
451
void janus_videocall_destroy(void) {
452
        if(!g_atomic_int_get(&initialized))
453
                return;
454
        g_atomic_int_set(&stopping, 1);
455
        if(handler_thread != NULL) {
456
                g_thread_join(handler_thread);
457
                handler_thread = NULL;
458
        }
459
        if(watchdog != NULL) {
460
                g_thread_join(watchdog);
461
                watchdog = NULL;
462
        }
463
        /* FIXME We should destroy the sessions cleanly */
464
        janus_mutex_lock(&sessions_mutex);
465
        g_hash_table_destroy(sessions);
466
        janus_mutex_unlock(&sessions_mutex);
467
        g_async_queue_unref(messages);
468
        messages = NULL;
469
        sessions = NULL;
470
        g_atomic_int_set(&initialized, 0);
471
        g_atomic_int_set(&stopping, 0);
472
        JANUS_LOG(LOG_INFO, "%s destroyed!\n", JANUS_VIDEOCALL_NAME);
473
}
474

    
475
int janus_videocall_get_api_compatibility(void) {
476
        /* Important! This is what your plugin MUST always return: don't lie here or bad things will happen */
477
        return JANUS_PLUGIN_API_VERSION;
478
}
479

    
480
int janus_videocall_get_version(void) {
481
        return JANUS_VIDEOCALL_VERSION;
482
}
483

    
484
const char *janus_videocall_get_version_string(void) {
485
        return JANUS_VIDEOCALL_VERSION_STRING;
486
}
487

    
488
const char *janus_videocall_get_description(void) {
489
        return JANUS_VIDEOCALL_DESCRIPTION;
490
}
491

    
492
const char *janus_videocall_get_name(void) {
493
        return JANUS_VIDEOCALL_NAME;
494
}
495

    
496
const char *janus_videocall_get_author(void) {
497
        return JANUS_VIDEOCALL_AUTHOR;
498
}
499

    
500
const char *janus_videocall_get_package(void) {
501
        return JANUS_VIDEOCALL_PACKAGE;
502
}
503

    
504
void janus_videocall_create_session(janus_plugin_session *handle, int *error) {
505
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized)) {
506
                *error = -1;
507
                return;
508
        }        
509
        janus_videocall_session *session = (janus_videocall_session *)calloc(1, sizeof(janus_videocall_session));
510
        if(session == NULL) {
511
                JANUS_LOG(LOG_FATAL, "Memory error!\n");
512
                *error = -2;
513
                return;
514
        }
515
        session->handle = handle;
516
        session->audio_active = TRUE;
517
        session->video_active = TRUE;
518
        session->bitrate = 0;        /* No limit */
519
        session->peer = NULL;
520
        session->username = NULL;
521
        session->destroyed = 0;
522
        handle->plugin_handle = session;
523

    
524
        return;
525
}
526

    
527
void janus_videocall_destroy_session(janus_plugin_session *handle, int *error) {
528
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized)) {
529
                *error = -1;
530
                return;
531
        }
532
        janus_videocall_session *session = (janus_videocall_session *)handle->plugin_handle; 
533
        if(!session) {
534
                JANUS_LOG(LOG_ERR, "No VideoCall session associated with this handle...\n");
535
                *error = -2;
536
                return;
537
        }
538
        janus_mutex_lock(&sessions_mutex);
539
        if(!session->destroyed) {
540
                JANUS_LOG(LOG_VERB, "Removing VideoCall user %s session...\n", session->username ? session->username : "'unknown'");
541
                janus_videocall_hangup_media(handle);
542
                session->destroyed = janus_get_monotonic_time();
543
                if(session->username != NULL) {
544
                        int res = g_hash_table_remove(sessions, (gpointer)session->username);
545
                        JANUS_LOG(LOG_VERB, "  -- Removed: %d\n", res);
546
                }
547
                /* Cleaning up and removing the session is done in a lazy way */
548
                old_sessions = g_list_append(old_sessions, session);
549
        }
550
        janus_mutex_unlock(&sessions_mutex);
551
        return;
552
}
553

    
554
char *janus_videocall_query_session(janus_plugin_session *handle) {
555
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized)) {
556
                return NULL;
557
        }        
558
        janus_videocall_session *session = (janus_videocall_session *)handle->plugin_handle;
559
        if(!session) {
560
                JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
561
                return NULL;
562
        }
563
        /* Provide some generic info, e.g., if we're in a call and with whom */
564
        json_t *info = json_object();
565
        json_object_set_new(info, "state", json_string(session->peer ? "incall" : "idle"));
566
        json_object_set_new(info, "username", session->username ? json_string(session->username) : NULL);
567
        if(session->peer) {
568
                json_object_set_new(info, "peer", session->peer->username ? json_string(session->peer->username) : NULL);
569
                json_object_set_new(info, "audio_active", json_string(session->audio_active ? "true" : "false"));
570
                json_object_set_new(info, "video_active", json_string(session->video_active ? "true" : "false"));
571
                json_object_set_new(info, "bitrate", json_integer(session->bitrate));
572
                json_object_set_new(info, "slowlink_count", json_integer(session->slowlink_count));
573
        }
574
        json_object_set_new(info, "destroyed", json_integer(session->destroyed));
575
        char *info_text = json_dumps(info, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
576
        json_decref(info);
577
        return info_text;
578
}
579

    
580
struct janus_plugin_result *janus_videocall_handle_message(janus_plugin_session *handle, char *transaction, char *message, char *sdp_type, char *sdp) {
581
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
582
                return janus_plugin_result_new(JANUS_PLUGIN_ERROR, g_atomic_int_get(&stopping) ? "Shutting down" : "Plugin not initialized");
583
        janus_videocall_message *msg = calloc(1, sizeof(janus_videocall_message));
584
        if(msg == NULL) {
585
                JANUS_LOG(LOG_FATAL, "Memory error!\n");
586
                return janus_plugin_result_new(JANUS_PLUGIN_ERROR, "Memory error");
587
        }
588
        msg->handle = handle;
589
        msg->transaction = transaction;
590
        msg->message = message;
591
        msg->sdp_type = sdp_type;
592
        msg->sdp = sdp;
593
        g_async_queue_push(messages, msg);
594

    
595
        /* All the requests to this plugin are handled asynchronously */
596
        return janus_plugin_result_new(JANUS_PLUGIN_OK_WAIT, NULL);
597
}
598

    
599
void janus_videocall_setup_media(janus_plugin_session *handle) {
600
        JANUS_LOG(LOG_INFO, "WebRTC media is now available\n");
601
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
602
                return;
603
        janus_videocall_session *session = (janus_videocall_session *)handle->plugin_handle;        
604
        if(!session) {
605
                JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
606
                return;
607
        }
608
        if(session->destroyed)
609
                return;
610
        /* We really don't care, as we only relay RTP/RTCP we get in the first place anyway */
611
}
612

    
613
void janus_videocall_incoming_rtp(janus_plugin_session *handle, int video, char *buf, int len) {
614
        if(handle == NULL || handle->stopped || g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
615
                return;
616
        if(gateway) {
617
                /* Honour the audio/video active flags */
618
                janus_videocall_session *session = (janus_videocall_session *)handle->plugin_handle;        
619
                if(!session) {
620
                        JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
621
                        return;
622
                }
623
                if(!session->peer) {
624
                        JANUS_LOG(LOG_ERR, "Session has no peer...\n");
625
                        return;
626
                }
627
                if(session->destroyed || session->peer->destroyed)
628
                        return;
629
                if((!video && session->audio_active) || (video && session->video_active)) {
630
                        gateway->relay_rtp(session->peer->handle, video, buf, len);
631
                }
632
        }
633
}
634

    
635
void janus_videocall_incoming_rtcp(janus_plugin_session *handle, int video, char *buf, int len) {
636
        if(handle == NULL || handle->stopped || g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
637
                return;
638
        if(gateway) {
639
                janus_videocall_session *session = (janus_videocall_session *)handle->plugin_handle;        
640
                if(!session) {
641
                        JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
642
                        return;
643
                }
644
                if(!session->peer) {
645
                        JANUS_LOG(LOG_ERR, "Session has no peer...\n");
646
                        return;
647
                }
648
                if(session->destroyed || session->peer->destroyed)
649
                        return;
650
                if(session->bitrate > 0)
651
                        janus_rtcp_cap_remb(buf, len, session->bitrate);
652
                gateway->relay_rtcp(session->peer->handle, video, buf, len);
653
        }
654
}
655

    
656
void janus_videocall_incoming_data(janus_plugin_session *handle, char *buf, int len) {
657
        if(handle == NULL || handle->stopped || g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
658
                return;
659
        if(gateway) {
660
                janus_videocall_session *session = (janus_videocall_session *)handle->plugin_handle;        
661
                if(!session) {
662
                        JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
663
                        return;
664
                }
665
                if(!session->peer) {
666
                        JANUS_LOG(LOG_ERR, "Session has no peer...\n");
667
                        return;
668
                }
669
                if(session->destroyed || session->peer->destroyed)
670
                        return;
671
                if(buf == NULL || len <= 0)
672
                        return;
673
                char text[1<<16];
674
                memset(text, 0, 1<<16);
675
                memcpy(text, buf, len);
676
                text[len] = '\0';
677
                JANUS_LOG(LOG_VERB, "Got a DataChannel message (%zu bytes) to forward: %s\n", strlen(text), text);
678
                gateway->relay_data(session->peer->handle, text, strlen(text));
679
        }
680
}
681

    
682
void janus_videocall_slow_link(janus_plugin_session *handle, int uplink, int video) {
683
        /* The core is informing us that our peer got or sent too many NACKs, are we pushing media too hard? */
684
        if(handle == NULL || handle->stopped || g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
685
                return;
686
        janus_videocall_session *session = (janus_videocall_session *)handle->plugin_handle;        
687
        if(!session) {
688
                JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
689
                return;
690
        }
691
        if(session->destroyed)
692
                return;
693
        session->slowlink_count++;
694
        if(uplink && !video && !session->audio_active) {
695
                /* We're not relaying audio and the peer is expecting it, so NACKs are normal */
696
                JANUS_LOG(LOG_VERB, "Getting a lot of NACKs (slow uplink) for audio, but that's expected, a configure disabled the audio forwarding\n");
697
        } else if(uplink && video && !session->video_active) {
698
                /* We're not relaying video and the peer is expecting it, so NACKs are normal */
699
                JANUS_LOG(LOG_VERB, "Getting a lot of NACKs (slow uplink) for video, but that's expected, a configure disabled the video forwarding\n");
700
        } else {
701
                /* Slow uplink or downlink, maybe we set the bitrate cap too high? */
702
                if(video) {
703
                        /* Halve the bitrate, but don't go too low... */
704
                        if(!uplink) {
705
                                /* Downlink issue, user has trouble sending, halve this user's bitrate cap */
706
                                session->bitrate = session->bitrate > 0 ? session->bitrate : 512*1024;
707
                                session->bitrate = session->bitrate/2;
708
                                if(session->bitrate < 64*1024)
709
                                        session->bitrate = 64*1024;
710
                        } else {
711
                                /* Uplink issue, user has trouble receiving, halve this user's peer's bitrate cap */
712
                                if(session->peer == NULL || session->peer->handle == NULL)
713
                                        return;        /* Nothing to do */
714
                                session->peer->bitrate = session->peer->bitrate > 0 ? session->peer->bitrate : 512*1024;
715
                                session->peer->bitrate = session->peer->bitrate/2;
716
                                if(session->peer->bitrate < 64*1024)
717
                                        session->peer->bitrate = 64*1024;
718
                        }
719
                        JANUS_LOG(LOG_WARN, "Getting a lot of NACKs (slow %s) for %s, forcing a lower REMB: %"SCNu64"\n",
720
                                uplink ? "uplink" : "downlink", video ? "video" : "audio", uplink ? session->peer->bitrate : session->bitrate);
721
                        /* ... and send a new REMB back */
722
                        char rtcpbuf[200];
723
                        memset(rtcpbuf, 0, 200);
724
                        /* FIXME First put a RR (fake)... */
725
                        int rrlen = 32;
726
                        rtcp_rr *rr = (rtcp_rr *)&rtcpbuf;
727
                        rr->header.version = 2;
728
                        rr->header.type = RTCP_RR;
729
                        rr->header.rc = 1;
730
                        rr->header.length = htons((rrlen/4)-1);
731
                        /* ... then put a SDES... */
732
                        int sdeslen = janus_rtcp_sdes((char *)(&rtcpbuf)+rrlen, 200-rrlen, "janusvideo", 10);
733
                        if(sdeslen > 0) {
734
                                /* ... and then finally a REMB */
735
                                janus_rtcp_remb((char *)(&rtcpbuf)+rrlen+sdeslen, 24, uplink ? session->peer->bitrate : session->bitrate);
736
                                gateway->relay_rtcp(uplink ? session->peer->handle : handle, 1, rtcpbuf, rrlen+sdeslen+24);
737
                        }
738
                        /* As a last thing, notify the affected user about this */
739
                        json_t *event = json_object();
740
                        json_object_set_new(event, "videocall", json_string("event"));
741
                        json_t *result = json_object();
742
                        json_object_set_new(result, "status", json_string("slow_link"));
743
                        json_object_set_new(result, "bitrate", json_integer(uplink ? session->peer->bitrate : session->bitrate));
744
                        json_object_set_new(event, "result", result);
745
                        char *event_text = json_dumps(event, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
746
                        json_decref(event);
747
                        json_decref(result);
748
                        event = NULL;
749
                        gateway->push_event(uplink ? session->peer->handle : handle, &janus_videocall_plugin, NULL, event_text, NULL, NULL);
750
                        g_free(event_text);
751
                }
752
        }
753
}
754

    
755
void janus_videocall_hangup_media(janus_plugin_session *handle) {
756
        JANUS_LOG(LOG_INFO, "No WebRTC media anymore\n");
757
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
758
                return;
759
        janus_videocall_session *session = (janus_videocall_session *)handle->plugin_handle;        
760
        if(!session) {
761
                JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
762
                return;
763
        }
764
        if(session->destroyed || session->hangingup)
765
                return;
766
        session->hangingup = TRUE;
767
        if(session->peer) {
768
                /* Send event to our peer too */
769
                json_t *call = json_object();
770
                json_object_set_new(call, "videocall", json_string("event"));
771
                json_t *calling = json_object();
772
                json_object_set_new(calling, "event", json_string("hangup"));
773
                json_object_set_new(calling, "username", json_string(session->username));
774
                json_object_set_new(calling, "reason", json_string("Remote hangup"));
775
                json_object_set_new(call, "result", calling);
776
                char *call_text = json_dumps(call, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
777
                json_decref(call);
778
                JANUS_LOG(LOG_VERB, "Pushing event to peer: %s\n", call_text);
779
                int ret = gateway->push_event(session->peer->handle, &janus_videocall_plugin, NULL, call_text, NULL, NULL);
780
                JANUS_LOG(LOG_VERB, "  >> %d (%s)\n", ret, janus_get_api_error(ret));
781
                g_free(call_text);
782
        }
783
        session->peer = NULL;
784
        /* Reset controls */
785
        session->audio_active = TRUE;
786
        session->video_active = TRUE;
787
        session->bitrate = 0;
788
        /* Done */
789
        session->hangingup = FALSE;
790
}
791

    
792
/* Thread to handle incoming messages */
793
static void *janus_videocall_handler(void *data) {
794
        JANUS_LOG(LOG_VERB, "Joining VideoCall handler thread\n");
795
        janus_videocall_message *msg = NULL;
796
        int error_code = 0;
797
        char *error_cause = calloc(512, sizeof(char));
798
        if(error_cause == NULL) {
799
                JANUS_LOG(LOG_FATAL, "Memory error!\n");
800
                return NULL;
801
        }
802
        json_t *root = NULL;
803
        while(g_atomic_int_get(&initialized) && !g_atomic_int_get(&stopping)) {
804
                if(!messages || (msg = g_async_queue_try_pop(messages)) == NULL) {
805
                        usleep(50000);
806
                        continue;
807
                }
808
                janus_videocall_session *session = (janus_videocall_session *)msg->handle->plugin_handle;        
809
                if(!session) {
810
                        JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
811
                        janus_videocall_message_free(msg);
812
                        continue;
813
                }
814
                if(session->destroyed) {
815
                        janus_videocall_message_free(msg);
816
                        continue;
817
                }
818
                /* Handle request */
819
                error_code = 0;
820
                root = NULL;
821
                JANUS_LOG(LOG_VERB, "Handling message: %s\n", msg->message);
822
                if(msg->message == NULL) {
823
                        JANUS_LOG(LOG_ERR, "No message??\n");
824
                        error_code = JANUS_VIDEOCALL_ERROR_NO_MESSAGE;
825
                        g_snprintf(error_cause, 512, "%s", "No message??");
826
                        goto error;
827
                }
828
                json_error_t error;
829
                root = json_loads(msg->message, 0, &error);
830
                if(!root) {
831
                        JANUS_LOG(LOG_ERR, "JSON error: on line %d: %s\n", error.line, error.text);
832
                        error_code = JANUS_VIDEOCALL_ERROR_INVALID_JSON;
833
                        g_snprintf(error_cause, 512, "JSON error: on line %d: %s", error.line, error.text);
834
                        goto error;
835
                }
836
                if(!json_is_object(root)) {
837
                        JANUS_LOG(LOG_ERR, "JSON error: not an object\n");
838
                        error_code = JANUS_VIDEOCALL_ERROR_INVALID_JSON;
839
                        g_snprintf(error_cause, 512, "JSON error: not an object");
840
                        goto error;
841
                }
842
                json_t *request = json_object_get(root, "request");
843
                if(!request) {
844
                        JANUS_LOG(LOG_ERR, "Missing element (request)\n");
845
                        error_code = JANUS_VIDEOCALL_ERROR_MISSING_ELEMENT;
846
                        g_snprintf(error_cause, 512, "Missing element (request)");
847
                        goto error;
848
                }
849
                if(!json_is_string(request)) {
850
                        JANUS_LOG(LOG_ERR, "Invalid element (request should be a string)\n");
851
                        error_code = JANUS_VIDEOCALL_ERROR_INVALID_ELEMENT;
852
                        g_snprintf(error_cause, 512, "Invalid element (request should be a string)");
853
                        goto error;
854
                }
855
                const char *request_text = json_string_value(request);
856
                json_t *result = NULL;
857
                char *sdp_type = NULL, *sdp = NULL;
858
                if(!strcasecmp(request_text, "list")) {
859
                        result = json_object();
860
                        json_t *list = json_array();
861
                        JANUS_LOG(LOG_VERB, "Request for the list of peers\n");
862
                        /* Return a list of all available mountpoints */
863
                        janus_mutex_lock(&sessions_mutex);
864
                        GHashTableIter iter;
865
                        gpointer value;
866
                        g_hash_table_iter_init(&iter, sessions);
867
                        while (g_hash_table_iter_next(&iter, NULL, &value)) {
868
                                janus_videocall_session *user = value;
869
                                if(user != NULL && user->username != NULL)
870
                                        json_array_append_new(list, json_string(user->username));
871
                        }
872
                        json_object_set_new(result, "list", list);
873
                        janus_mutex_unlock(&sessions_mutex);
874
                } else if(!strcasecmp(request_text, "register")) {
875
                        /* Map this handle to a username */
876
                        if(session->username != NULL) {
877
                                JANUS_LOG(LOG_ERR, "Already registered (%s)\n", session->username);
878
                                error_code = JANUS_VIDEOCALL_ERROR_ALREADY_REGISTERED;
879
                                g_snprintf(error_cause, 512, "Already registered (%s)", session->username);
880
                                goto error;
881
                        }
882
                        json_t *username = json_object_get(root, "username");
883
                        if(!username) {
884
                                JANUS_LOG(LOG_ERR, "Missing element (username)\n");
885
                                error_code = JANUS_VIDEOCALL_ERROR_MISSING_ELEMENT;
886
                                g_snprintf(error_cause, 512, "Missing element (username)");
887
                                goto error;
888
                        }
889
                        if(!json_is_string(username)) {
890
                                JANUS_LOG(LOG_ERR, "Invalid element (username should be a string)\n");
891
                                error_code = JANUS_VIDEOCALL_ERROR_INVALID_ELEMENT;
892
                                g_snprintf(error_cause, 512, "Invalid element (username should be a string)");
893
                                goto error;
894
                        }
895
                        const char *username_text = json_string_value(username);
896
                        janus_mutex_lock(&sessions_mutex);
897
                        if(g_hash_table_lookup(sessions, username_text) != NULL) {
898
                                janus_mutex_unlock(&sessions_mutex);
899
                                JANUS_LOG(LOG_ERR, "Username '%s' already taken\n", username_text);
900
                                error_code = JANUS_VIDEOCALL_ERROR_USERNAME_TAKEN;
901
                                g_snprintf(error_cause, 512, "Username '%s' already taken", username_text);
902
                                goto error;
903
                        }
904
                        janus_mutex_unlock(&sessions_mutex);
905
                        session->username = g_strdup(username_text);
906
                        if(session->username == NULL) {
907
                                JANUS_LOG(LOG_FATAL, "Memory error!\n");
908
                                error_code = JANUS_VIDEOCALL_ERROR_UNKNOWN_ERROR;
909
                                g_snprintf(error_cause, 512, "Memory error");
910
                                goto error;
911
                        }
912
                        janus_mutex_lock(&sessions_mutex);
913
                        g_hash_table_insert(sessions, (gpointer)session->username, session);
914
                        janus_mutex_unlock(&sessions_mutex);
915
                        result = json_object();
916
                        json_object_set_new(result, "event", json_string("registered"));
917
                        json_object_set_new(result, "username", json_string(username_text));
918
                } else if(!strcasecmp(request_text, "call")) {
919
                        /* Call another peer */
920
                        if(session->username == NULL) {
921
                                JANUS_LOG(LOG_ERR, "Register a username first\n");
922
                                error_code = JANUS_VIDEOCALL_ERROR_REGISTER_FIRST;
923
                                g_snprintf(error_cause, 512, "Register a username first");
924
                                goto error;
925
                        }
926
                        if(session->peer != NULL) {
927
                                JANUS_LOG(LOG_ERR, "Already in a call\n");
928
                                error_code = JANUS_VIDEOCALL_ERROR_ALREADY_IN_CALL;
929
                                g_snprintf(error_cause, 512, "Already in a call");
930
                                goto error;
931
                        }
932
                        json_t *username = json_object_get(root, "username");
933
                        if(!username) {
934
                                JANUS_LOG(LOG_ERR, "Missing element (username)\n");
935
                                error_code = JANUS_VIDEOCALL_ERROR_MISSING_ELEMENT;
936
                                g_snprintf(error_cause, 512, "Missing element (username)");
937
                                goto error;
938
                        }
939
                        if(!json_is_string(username)) {
940
                                JANUS_LOG(LOG_ERR, "Invalid element (username should be a string)\n");
941
                                error_code = JANUS_VIDEOCALL_ERROR_INVALID_ELEMENT;
942
                                g_snprintf(error_cause, 512, "Invalid element (username should be a string)");
943
                                goto error;
944
                        }
945
                        const char *username_text = json_string_value(username);
946
                        if(!strcmp(username_text, session->username)) {
947
                                JANUS_LOG(LOG_ERR, "You can't call yourself... use the EchoTest for that\n");
948
                                error_code = JANUS_VIDEOCALL_ERROR_USE_ECHO_TEST;
949
                                g_snprintf(error_cause, 512, "You can't call yourself... use the EchoTest for that");
950
                                goto error;
951
                        }
952
                        janus_mutex_lock(&sessions_mutex);
953
                        janus_videocall_session *peer = g_hash_table_lookup(sessions, username_text);
954
                        if(peer == NULL || peer->destroyed) {
955
                                janus_mutex_unlock(&sessions_mutex);
956
                                JANUS_LOG(LOG_ERR, "Username '%s' doesn't exist\n", username_text);
957
                                error_code = JANUS_VIDEOCALL_ERROR_NO_SUCH_USERNAME;
958
                                g_snprintf(error_cause, 512, "Username '%s' doesn't exist", username_text);
959
                                goto error;
960
                        }
961
                        if(peer->peer != NULL) {
962
                                janus_mutex_unlock(&sessions_mutex);
963
                                JANUS_LOG(LOG_VERB, "%s is busy\n", username_text);
964
                                result = json_object();
965
                                json_object_set_new(result, "event", json_string("hangup"));
966
                                json_object_set_new(result, "username", json_string(session->username));
967
                                json_object_set_new(result, "reason", json_string("User busy"));
968
                        } else {
969
                                janus_mutex_unlock(&sessions_mutex);
970
                                /* Any SDP to handle? if not, something's wrong */
971
                                if(!msg->sdp) {
972
                                        JANUS_LOG(LOG_ERR, "Missing SDP\n");
973
                                        error_code = JANUS_VIDEOCALL_ERROR_MISSING_SDP;
974
                                        g_snprintf(error_cause, 512, "Missing SDP");
975
                                        goto error;
976
                                }
977
                                janus_mutex_lock(&sessions_mutex);
978
                                session->peer = peer;
979
                                peer->peer = session;
980
                                janus_mutex_unlock(&sessions_mutex);
981
                                JANUS_LOG(LOG_VERB, "%s is calling %s\n", session->username, session->peer->username);
982
                                JANUS_LOG(LOG_VERB, "This is involving a negotiation (%s) as well:\n%s\n", msg->sdp_type, msg->sdp);
983
                                /* Send SDP to our peer */
984
                                json_t *call = json_object();
985
                                json_object_set_new(call, "videocall", json_string("event"));
986
                                json_t *calling = json_object();
987
                                json_object_set_new(calling, "event", json_string("incomingcall"));
988
                                json_object_set_new(calling, "username", json_string(session->username));
989
                                json_object_set_new(call, "result", calling);
990
                                char *call_text = json_dumps(call, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
991
                                json_decref(call);
992
                                JANUS_LOG(LOG_VERB, "Pushing event to peer: %s\n", call_text);
993
                                int ret = gateway->push_event(peer->handle, &janus_videocall_plugin, NULL, call_text, msg->sdp_type, msg->sdp);
994
                                JANUS_LOG(LOG_VERB, "  >> %d (%s)\n", ret, janus_get_api_error(ret));
995
                                g_free(call_text);
996
                                /* Send an ack back */
997
                                result = json_object();
998
                                json_object_set_new(result, "event", json_string("calling"));
999
                        }
1000
                } else if(!strcasecmp(request_text, "accept")) {
1001
                        /* Accept a call from another peer */
1002
                        if(session->peer == NULL) {
1003
                                JANUS_LOG(LOG_ERR, "No incoming call to accept\n");
1004
                                error_code = JANUS_VIDEOCALL_ERROR_NO_CALL;
1005
                                g_snprintf(error_cause, 512, "No incoming call to accept");
1006
                                goto error;
1007
                        }
1008
                        /* Any SDP to handle? if not, something's wrong */
1009
                        if(!msg->sdp) {
1010
                                JANUS_LOG(LOG_ERR, "Missing SDP\n");
1011
                                error_code = JANUS_VIDEOCALL_ERROR_MISSING_SDP;
1012
                                g_snprintf(error_cause, 512, "Missing SDP");
1013
                                goto error;
1014
                        }
1015
                        JANUS_LOG(LOG_VERB, "%s is accepting a call from %s\n", session->username, session->peer->username);
1016
                        JANUS_LOG(LOG_VERB, "This is involving a negotiation (%s) as well:\n%s\n", msg->sdp_type, msg->sdp);
1017
                        /* Send SDP to our peer */
1018
                        json_t *call = json_object();
1019
                        json_object_set_new(call, "videocall", json_string("event"));
1020
                        json_t *calling = json_object();
1021
                        json_object_set_new(calling, "event", json_string("accepted"));
1022
                        json_object_set_new(calling, "username", json_string(session->username));
1023
                        json_object_set_new(call, "result", calling);
1024
                        char *call_text = json_dumps(call, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1025
                        json_decref(call);
1026
                        JANUS_LOG(LOG_VERB, "Pushing event to peer: %s\n", call_text);
1027
                        int ret = gateway->push_event(session->peer->handle, &janus_videocall_plugin, NULL, call_text, msg->sdp_type, msg->sdp);
1028
                        JANUS_LOG(LOG_VERB, "  >> %d (%s)\n", ret, janus_get_api_error(ret));
1029
                        g_free(call_text);
1030
                        /* Send an ack back */
1031
                        result = json_object();
1032
                        json_object_set_new(result, "event", json_string("accepted"));
1033
                } else if(!strcasecmp(request_text, "set")) {
1034
                        /* Update the local configuration (audio/video mute/unmute, or bitrate cap) */
1035
                        json_t *audio = json_object_get(root, "audio");
1036
                        if(audio && !json_is_boolean(audio)) {
1037
                                JANUS_LOG(LOG_ERR, "Invalid element (audio should be a boolean)\n");
1038
                                error_code = JANUS_VIDEOCALL_ERROR_INVALID_ELEMENT;
1039
                                g_snprintf(error_cause, 512, "Invalid value (audio should be a boolean)");
1040
                                goto error;
1041
                        }
1042
                        json_t *video = json_object_get(root, "video");
1043
                        if(video && !json_is_boolean(video)) {
1044
                                JANUS_LOG(LOG_ERR, "Invalid element (video should be a boolean)\n");
1045
                                error_code = JANUS_VIDEOCALL_ERROR_INVALID_ELEMENT;
1046
                                g_snprintf(error_cause, 512, "Invalid value (video should be a boolean)");
1047
                                goto error;
1048
                        }
1049
                        json_t *bitrate = json_object_get(root, "bitrate");
1050
                        if(bitrate && (!json_is_integer(bitrate) || json_integer_value(bitrate) < 0)) {
1051
                                JANUS_LOG(LOG_ERR, "Invalid element (bitrate should be a positive integer)\n");
1052
                                error_code = JANUS_VIDEOCALL_ERROR_INVALID_ELEMENT;
1053
                                g_snprintf(error_cause, 512, "Invalid value (bitrate should be a positive integer)");
1054
                                goto error;
1055
                        }
1056
                        if(audio) {
1057
                                session->audio_active = json_is_true(audio);
1058
                                JANUS_LOG(LOG_VERB, "Setting audio property: %s\n", session->audio_active ? "true" : "false");
1059
                        }
1060
                        if(video) {
1061
                                if(!session->video_active && json_is_true(video)) {
1062
                                        /* Send a PLI */
1063
                                        JANUS_LOG(LOG_VERB, "Just (re-)enabled video, sending a PLI to recover it\n");
1064
                                        char buf[12];
1065
                                        memset(buf, 0, 12);
1066
                                        janus_rtcp_pli((char *)&buf, 12);
1067
                                        gateway->relay_rtcp(session->handle, 1, buf, 12);
1068
                                }
1069
                                session->video_active = json_is_true(video);
1070
                                JANUS_LOG(LOG_VERB, "Setting video property: %s\n", session->video_active ? "true" : "false");
1071
                        }
1072
                        if(bitrate) {
1073
                                session->bitrate = json_integer_value(bitrate);
1074
                                JANUS_LOG(LOG_VERB, "Setting video bitrate: %"SCNu64"\n", session->bitrate);
1075
                                if(session->bitrate > 0) {
1076
                                        /* FIXME Generate a new REMB (especially useful for Firefox, which doesn't send any we can cap later) */
1077
                                        char buf[24];
1078
                                        memset(buf, 0, 24);
1079
                                        janus_rtcp_remb((char *)&buf, 24, session->bitrate);
1080
                                        JANUS_LOG(LOG_VERB, "Sending REMB\n");
1081
                                        gateway->relay_rtcp(session->handle, 1, buf, 24);
1082
                                        /* FIXME How should we handle a subsequent "no limit" bitrate? */
1083
                                }
1084
                        }
1085
                        /* Send an ack back */
1086
                        result = json_object();
1087
                        json_object_set_new(result, "event", json_string("set"));
1088
                } else if(!strcasecmp(request_text, "hangup")) {
1089
                        /* Hangup an ongoing call or reject an incoming one */
1090
                        janus_mutex_lock(&sessions_mutex);
1091
                        janus_videocall_session *peer = session->peer;
1092
                        if(peer == NULL) {
1093
                                JANUS_LOG(LOG_WARN, "No call to hangup\n");
1094
                        } else {
1095
                                JANUS_LOG(LOG_VERB, "%s is hanging up the call with %s\n", session->username, peer->username);
1096
                                session->peer = NULL;
1097
                                peer->peer = NULL;
1098
                        }
1099
                        janus_mutex_unlock(&sessions_mutex);
1100
                        /* Notify the success as an hangup message */
1101
                        result = json_object();
1102
                        json_object_set_new(result, "event", json_string("hangup"));
1103
                        json_object_set_new(result, "username", json_string(session->username));
1104
                        json_object_set_new(result, "reason", json_string("We did the hangup"));
1105
                        if(peer != NULL) {
1106
                                /* Send event to our peer too */
1107
                                json_t *call = json_object();
1108
                                json_object_set_new(call, "videocall", json_string("event"));
1109
                                json_t *calling = json_object();
1110
                                json_object_set_new(calling, "event", json_string("hangup"));
1111
                                json_object_set_new(calling, "username", json_string(session->username));
1112
                                json_object_set_new(calling, "reason", json_string("Remote hangup"));
1113
                                json_object_set_new(call, "result", calling);
1114
                                char *call_text = json_dumps(call, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1115
                                json_decref(call);
1116
                                JANUS_LOG(LOG_VERB, "Pushing event to peer: %s\n", call_text);
1117
                                int ret = gateway->push_event(peer->handle, &janus_videocall_plugin, NULL, call_text, NULL, NULL);
1118
                                JANUS_LOG(LOG_VERB, "  >> %d (%s)\n", ret, janus_get_api_error(ret));
1119
                                g_free(call_text);
1120
                        }
1121
                } else {
1122
                        JANUS_LOG(LOG_ERR, "Unknown request (%s)\n", request_text);
1123
                        error_code = JANUS_VIDEOCALL_ERROR_INVALID_REQUEST;
1124
                        g_snprintf(error_cause, 512, "Unknown request (%s)", request_text);
1125
                        goto error;
1126
                }
1127

    
1128
                json_decref(root);
1129
                /* Prepare JSON event */
1130
                json_t *event = json_object();
1131
                json_object_set_new(event, "videocall", json_string("event"));
1132
                if(result != NULL)
1133
                        json_object_set_new(event, "result", result);
1134
                char *event_text = json_dumps(event, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1135
                json_decref(event);
1136
                JANUS_LOG(LOG_VERB, "Pushing event: %s\n", event_text);
1137
                int ret = gateway->push_event(msg->handle, &janus_videocall_plugin, msg->transaction, event_text, sdp_type, sdp);
1138
                JANUS_LOG(LOG_VERB, "  >> %d (%s)\n", ret, janus_get_api_error(ret));
1139
                g_free(event_text);
1140
                if(sdp)
1141
                        g_free(sdp);
1142
                janus_videocall_message_free(msg);
1143
                continue;
1144
                
1145
error:
1146
                {
1147
                        if(root != NULL)
1148
                                json_decref(root);
1149
                        /* Prepare JSON error event */
1150
                        json_t *event = json_object();
1151
                        json_object_set_new(event, "videocall", json_string("event"));
1152
                        json_object_set_new(event, "error_code", json_integer(error_code));
1153
                        json_object_set_new(event, "error", json_string(error_cause));
1154
                        char *event_text = json_dumps(event, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1155
                        json_decref(event);
1156
                        JANUS_LOG(LOG_VERB, "Pushing event: %s\n", event_text);
1157
                        int ret = gateway->push_event(msg->handle, &janus_videocall_plugin, msg->transaction, event_text, NULL, NULL);
1158
                        JANUS_LOG(LOG_VERB, "  >> %d (%s)\n", ret, janus_get_api_error(ret));
1159
                        g_free(event_text);
1160
                        janus_videocall_message_free(msg);
1161
                }
1162
        }
1163
        g_free(error_cause);
1164
        JANUS_LOG(LOG_VERB, "Leaving VideoCall handler thread\n");
1165
        return NULL;
1166
}