Statistics
| Branch: | Revision:

janus-gateway / plugins / janus_videocall.c @ 83e899f3

History | View | Annotate | Download (42 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
        guint64 destroyed;        /* Time at which this session was marked as destroyed */
338
} janus_videocall_session;
339
static GHashTable *sessions;
340
static GList *old_sessions;
341
static janus_mutex sessions_mutex;
342

    
343

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

    
360

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

    
400

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

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

    
429
        g_atomic_int_set(&initialized, 1);
430

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

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

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

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

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

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

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

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

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

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

    
523
        return;
524
}
525

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

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

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

    
599
        /* All the requests to this plugin are handled asynchronously */
600
        return janus_plugin_result_new(JANUS_PLUGIN_OK_WAIT, NULL);
601
}
602

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

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

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

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

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

    
743
                }
744
        }
745
}
746

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

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

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