Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (51.7 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. All the attributes (except \c request) are
166
 * optional, so any request can contain a subset of them:
167
 *
168
\verbatim
169
{
170
        "request" : "set",
171
        "audio" : true|false,
172
        "video" : true|false,
173
        "bitrate" : <numeric bitrate value>,
174
        "record" : true|false,
175
        "filename" : <base path/filename to use for the recording>
176
}
177
\endverbatim
178
 *
179
 * \c audio instructs the plugin to do or do not relay audio frames;
180
 * \c video does the same for video; \c bitrate caps the bandwidth to
181
 * force on the browser encoding side (e.g., 128000 for 128kbps);
182
 * \c record enables or disables the recording of this peer; in case
183
 * recording is enabled, \c filename allows to specify a base
184
 * path/filename to use for the files (-audio.mjr and -video.mjr are
185
 * automatically appended). Beware that enabling the recording only
186
 * records this user's contribution, and not the whole call: to record
187
 * both sides, you need to enable recording for both the peers in the
188
 * call.
189
 * 
190
 * A successful request will result in a \c set event:
191
 * 
192
\verbatim
193
{
194
        "videocall" : "event",
195
        "result" : {
196
                "event" : "set"
197
        }
198
}
199
\endverbatim
200
 * 
201
 * To decline an incoming call, cancel an attempt to call or simply
202
 * hangup an ongoing conversation, the \c hangup request can be used,
203
 * which has to be formatted as follows:
204
 * 
205
\verbatim
206
{
207
        "request" : "hangup"
208
}
209
\endverbatim
210
 *
211
 * Whatever the reason of a call being closed (e.g., a \c hangup request,
212
 * a PeerConnection being closed, or something else), both parties in
213
 * the communication will receive a \c hangup event:
214
 * 
215
\verbatim
216
{
217
        "videocall" : "event",
218
        "result" : {
219
                "event" : "hangup",
220
                "username" : "<username of who closed the communication>",
221
                "reason" : "<description of what happened>"
222
        }
223
}
224
\endverbatim
225
 * 
226
 * \ingroup plugins
227
 * \ref plugins
228
 */
229

    
230
#include "plugin.h"
231

    
232
#include <jansson.h>
233

    
234
#include "../debug.h"
235
#include "../apierror.h"
236
#include "../config.h"
237
#include "../mutex.h"
238
#include "../record.h"
239
#include "../rtcp.h"
240
#include "../utils.h"
241

    
242

    
243
/* Plugin information */
244
#define JANUS_VIDEOCALL_VERSION                        5
245
#define JANUS_VIDEOCALL_VERSION_STRING        "0.0.5"
246
#define JANUS_VIDEOCALL_DESCRIPTION                "This is a simple video call plugin for Janus, allowing two WebRTC peers to call each other through the gateway."
247
#define JANUS_VIDEOCALL_NAME                        "JANUS VideoCall plugin"
248
#define JANUS_VIDEOCALL_AUTHOR                        "Meetecho s.r.l."
249
#define JANUS_VIDEOCALL_PACKAGE                        "janus.plugin.videocall"
250

    
251
/* Plugin methods */
252
janus_plugin *create(void);
253
int janus_videocall_init(janus_callbacks *callback, const char *config_path);
254
void janus_videocall_destroy(void);
255
int janus_videocall_get_api_compatibility(void);
256
int janus_videocall_get_version(void);
257
const char *janus_videocall_get_version_string(void);
258
const char *janus_videocall_get_description(void);
259
const char *janus_videocall_get_name(void);
260
const char *janus_videocall_get_author(void);
261
const char *janus_videocall_get_package(void);
262
void janus_videocall_create_session(janus_plugin_session *handle, int *error);
263
struct janus_plugin_result *janus_videocall_handle_message(janus_plugin_session *handle, char *transaction, char *message, char *sdp_type, char *sdp);
264
void janus_videocall_setup_media(janus_plugin_session *handle);
265
void janus_videocall_incoming_rtp(janus_plugin_session *handle, int video, char *buf, int len);
266
void janus_videocall_incoming_rtcp(janus_plugin_session *handle, int video, char *buf, int len);
267
void janus_videocall_incoming_data(janus_plugin_session *handle, char *buf, int len);
268
void janus_videocall_slow_link(janus_plugin_session *handle, int uplink, int video);
269
void janus_videocall_hangup_media(janus_plugin_session *handle);
270
void janus_videocall_destroy_session(janus_plugin_session *handle, int *error);
271
char *janus_videocall_query_session(janus_plugin_session *handle);
272

    
273
/* Plugin setup */
274
static janus_plugin janus_videocall_plugin =
275
        JANUS_PLUGIN_INIT (
276
                .init = janus_videocall_init,
277
                .destroy = janus_videocall_destroy,
278

    
279
                .get_api_compatibility = janus_videocall_get_api_compatibility,
280
                .get_version = janus_videocall_get_version,
281
                .get_version_string = janus_videocall_get_version_string,
282
                .get_description = janus_videocall_get_description,
283
                .get_name = janus_videocall_get_name,
284
                .get_author = janus_videocall_get_author,
285
                .get_package = janus_videocall_get_package,
286
                
287
                .create_session = janus_videocall_create_session,
288
                .handle_message = janus_videocall_handle_message,
289
                .setup_media = janus_videocall_setup_media,
290
                .incoming_rtp = janus_videocall_incoming_rtp,
291
                .incoming_rtcp = janus_videocall_incoming_rtcp,
292
                .incoming_data = janus_videocall_incoming_data,
293
                .slow_link = janus_videocall_slow_link,
294
                .hangup_media = janus_videocall_hangup_media,
295
                .destroy_session = janus_videocall_destroy_session,
296
                .query_session = janus_videocall_query_session,
297
        );
298

    
299
/* Plugin creator */
300
janus_plugin *create(void) {
301
        JANUS_LOG(LOG_VERB, "%s created!\n", JANUS_VIDEOCALL_NAME);
302
        return &janus_videocall_plugin;
303
}
304

    
305
/* Parameter validation */
306
static struct janus_json_parameter request_parameters[] = {
307
        {"request", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
308
};
309
static struct janus_json_parameter username_parameters[] = {
310
        {"username", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
311
};
312
static struct janus_json_parameter set_parameters[] = {
313
        {"audio", JANUS_JSON_BOOL, 0},
314
        {"video", JANUS_JSON_BOOL, 0},
315
        {"bitrate", JSON_INTEGER, JANUS_JSON_PARAM_POSITIVE},
316
        {"record", JANUS_JSON_BOOL, 0},
317
        {"filename", JSON_STRING, 0}
318
};
319

    
320
/* Useful stuff */
321
static volatile gint initialized = 0, stopping = 0;
322
static gboolean notify_events = TRUE;
323
static janus_callbacks *gateway = NULL;
324
static GThread *handler_thread;
325
static GThread *watchdog;
326
static void *janus_videocall_handler(void *data);
327

    
328
typedef struct janus_videocall_message {
329
        janus_plugin_session *handle;
330
        char *transaction;
331
        char *message;
332
        char *sdp_type;
333
        char *sdp;
334
} janus_videocall_message;
335
static GAsyncQueue *messages = NULL;
336
static janus_videocall_message exit_message;
337

    
338
static void janus_videocall_message_free(janus_videocall_message *msg) {
339
        if(!msg || msg == &exit_message)
340
                return;
341

    
342
        msg->handle = NULL;
343

    
344
        g_free(msg->transaction);
345
        msg->transaction = NULL;
346
        g_free(msg->message);
347
        msg->message = NULL;
348
        g_free(msg->sdp_type);
349
        msg->sdp_type = NULL;
350
        g_free(msg->sdp);
351
        msg->sdp = NULL;
352

    
353
        g_free(msg);
354
}
355

    
356
typedef struct janus_videocall_session {
357
        janus_plugin_session *handle;
358
        gchar *username;
359
        gboolean has_audio;
360
        gboolean has_video;
361
        gboolean audio_active;
362
        gboolean video_active;
363
        uint64_t bitrate;
364
        guint16 slowlink_count;
365
        struct janus_videocall_session *peer;
366
        janus_recorder *arc;        /* The Janus recorder instance for this user's audio, if enabled */
367
        janus_recorder *vrc;        /* The Janus recorder instance for this user's video, if enabled */
368
        janus_mutex rec_mutex;        /* Mutex to protect the recorders from race conditions */
369
        volatile gint hangingup;
370
        gint64 destroyed;        /* Time at which this session was marked as destroyed */
371
} janus_videocall_session;
372
static GHashTable *sessions;
373
static GList *old_sessions;
374
static janus_mutex sessions_mutex;
375

    
376

    
377
/* Error codes */
378
#define JANUS_VIDEOCALL_ERROR_UNKNOWN_ERROR                        499
379
#define JANUS_VIDEOCALL_ERROR_NO_MESSAGE                        470
380
#define JANUS_VIDEOCALL_ERROR_INVALID_JSON                        471
381
#define JANUS_VIDEOCALL_ERROR_INVALID_REQUEST                472
382
#define JANUS_VIDEOCALL_ERROR_REGISTER_FIRST                473
383
#define JANUS_VIDEOCALL_ERROR_INVALID_ELEMENT                474
384
#define JANUS_VIDEOCALL_ERROR_MISSING_ELEMENT                475
385
#define JANUS_VIDEOCALL_ERROR_USERNAME_TAKEN                476
386
#define JANUS_VIDEOCALL_ERROR_ALREADY_REGISTERED        477
387
#define JANUS_VIDEOCALL_ERROR_NO_SUCH_USERNAME                478
388
#define JANUS_VIDEOCALL_ERROR_USE_ECHO_TEST                        479
389
#define JANUS_VIDEOCALL_ERROR_ALREADY_IN_CALL                480
390
#define JANUS_VIDEOCALL_ERROR_NO_CALL                                481
391
#define JANUS_VIDEOCALL_ERROR_MISSING_SDP                        482
392

    
393

    
394
/* VideoCall watchdog/garbage collector (sort of) */
395
void *janus_videocall_watchdog(void *data);
396
void *janus_videocall_watchdog(void *data) {
397
        JANUS_LOG(LOG_INFO, "VideoCall watchdog started\n");
398
        gint64 now = 0;
399
        while(g_atomic_int_get(&initialized) && !g_atomic_int_get(&stopping)) {
400
                janus_mutex_lock(&sessions_mutex);
401
                /* Iterate on all the sessions */
402
                now = janus_get_monotonic_time();
403
                if(old_sessions != NULL) {
404
                        GList *sl = old_sessions;
405
                        JANUS_LOG(LOG_HUGE, "Checking %d old VideoCall sessions...\n", g_list_length(old_sessions));
406
                        while(sl) {
407
                                janus_videocall_session *session = (janus_videocall_session *)sl->data;
408
                                if(!session) {
409
                                        sl = sl->next;
410
                                        continue;
411
                                }
412
                                if(now-session->destroyed >= 5*G_USEC_PER_SEC) {
413
                                        /* We're lazy and actually get rid of the stuff only after a few seconds */
414
                                        JANUS_LOG(LOG_VERB, "Freeing old VideoCall session\n");
415
                                        GList *rm = sl->next;
416
                                        old_sessions = g_list_delete_link(old_sessions, sl);
417
                                        sl = rm;
418
                                        session->handle = NULL;
419
                                        g_free(session);
420
                                        session = NULL;
421
                                        continue;
422
                                }
423
                                sl = sl->next;
424
                        }
425
                }
426
                janus_mutex_unlock(&sessions_mutex);
427
                g_usleep(500000);
428
        }
429
        JANUS_LOG(LOG_INFO, "VideoCall watchdog stopped\n");
430
        return NULL;
431
}
432

    
433

    
434
/* Plugin implementation */
435
int janus_videocall_init(janus_callbacks *callback, const char *config_path) {
436
        if(g_atomic_int_get(&stopping)) {
437
                /* Still stopping from before */
438
                return -1;
439
        }
440
        if(callback == NULL || config_path == NULL) {
441
                /* Invalid arguments */
442
                return -1;
443
        }
444

    
445
        /* Read configuration */
446
        char filename[255];
447
        g_snprintf(filename, 255, "%s/%s.cfg", config_path, JANUS_VIDEOCALL_PACKAGE);
448
        JANUS_LOG(LOG_VERB, "Configuration file: %s\n", filename);
449
        janus_config *config = janus_config_parse(filename);
450
        if(config != NULL) {
451
                janus_config_print(config);
452
                janus_config_item *events = janus_config_get_item_drilldown(config, "general", "events");
453
                if(events != NULL && events->value != NULL)
454
                        notify_events = janus_is_true(events->value);
455
                if(!notify_events && callback->events_is_enabled()) {
456
                        JANUS_LOG(LOG_WARN, "Notification of events to handlers disabled for %s\n", JANUS_VIDEOCALL_NAME);
457
                }
458
        }
459
        janus_config_destroy(config);
460
        config = NULL;
461
        
462
        sessions = g_hash_table_new(g_str_hash, g_str_equal);
463
        janus_mutex_init(&sessions_mutex);
464
        messages = g_async_queue_new_full((GDestroyNotify) janus_videocall_message_free);
465
        /* This is the callback we'll need to invoke to contact the gateway */
466
        gateway = callback;
467

    
468
        g_atomic_int_set(&initialized, 1);
469

    
470
        GError *error = NULL;
471
        /* Start the sessions watchdog */
472
        watchdog = g_thread_try_new("vcall watchdog", &janus_videocall_watchdog, NULL, &error);
473
        if(error != NULL) {
474
                g_atomic_int_set(&initialized, 0);
475
                JANUS_LOG(LOG_ERR, "Got error %d (%s) trying to launch the VideoCall watchdog thread...\n", error->code, error->message ? error->message : "??");
476
                return -1;
477
        }
478
        /* Launch the thread that will handle incoming messages */
479
        handler_thread = g_thread_try_new("janus videocall handler", janus_videocall_handler, NULL, &error);
480
        if(error != NULL) {
481
                g_atomic_int_set(&initialized, 0);
482
                JANUS_LOG(LOG_ERR, "Got error %d (%s) trying to launch the VideoCall handler thread...\n", error->code, error->message ? error->message : "??");
483
                return -1;
484
        }
485
        JANUS_LOG(LOG_INFO, "%s initialized!\n", JANUS_VIDEOCALL_NAME);
486
        return 0;
487
}
488

    
489
void janus_videocall_destroy(void) {
490
        if(!g_atomic_int_get(&initialized))
491
                return;
492
        g_atomic_int_set(&stopping, 1);
493

    
494
        g_async_queue_push(messages, &exit_message);
495
        if(handler_thread != NULL) {
496
                g_thread_join(handler_thread);
497
                handler_thread = NULL;
498
        }
499
        if(watchdog != NULL) {
500
                g_thread_join(watchdog);
501
                watchdog = NULL;
502
        }
503
        /* FIXME We should destroy the sessions cleanly */
504
        janus_mutex_lock(&sessions_mutex);
505
        g_hash_table_destroy(sessions);
506
        janus_mutex_unlock(&sessions_mutex);
507
        g_async_queue_unref(messages);
508
        messages = NULL;
509
        sessions = NULL;
510
        g_atomic_int_set(&initialized, 0);
511
        g_atomic_int_set(&stopping, 0);
512
        JANUS_LOG(LOG_INFO, "%s destroyed!\n", JANUS_VIDEOCALL_NAME);
513
}
514

    
515
int janus_videocall_get_api_compatibility(void) {
516
        /* Important! This is what your plugin MUST always return: don't lie here or bad things will happen */
517
        return JANUS_PLUGIN_API_VERSION;
518
}
519

    
520
int janus_videocall_get_version(void) {
521
        return JANUS_VIDEOCALL_VERSION;
522
}
523

    
524
const char *janus_videocall_get_version_string(void) {
525
        return JANUS_VIDEOCALL_VERSION_STRING;
526
}
527

    
528
const char *janus_videocall_get_description(void) {
529
        return JANUS_VIDEOCALL_DESCRIPTION;
530
}
531

    
532
const char *janus_videocall_get_name(void) {
533
        return JANUS_VIDEOCALL_NAME;
534
}
535

    
536
const char *janus_videocall_get_author(void) {
537
        return JANUS_VIDEOCALL_AUTHOR;
538
}
539

    
540
const char *janus_videocall_get_package(void) {
541
        return JANUS_VIDEOCALL_PACKAGE;
542
}
543

    
544
void janus_videocall_create_session(janus_plugin_session *handle, int *error) {
545
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized)) {
546
                *error = -1;
547
                return;
548
        }        
549
        janus_videocall_session *session = (janus_videocall_session *)g_malloc0(sizeof(janus_videocall_session));
550
        if(session == NULL) {
551
                JANUS_LOG(LOG_FATAL, "Memory error!\n");
552
                *error = -2;
553
                return;
554
        }
555
        session->handle = handle;
556
        session->has_audio = FALSE;
557
        session->has_video = FALSE;
558
        session->audio_active = TRUE;
559
        session->video_active = TRUE;
560
        session->bitrate = 0;        /* No limit */
561
        session->peer = NULL;
562
        session->username = NULL;
563
        janus_mutex_init(&session->rec_mutex);
564
        session->destroyed = 0;
565
        g_atomic_int_set(&session->hangingup, 0);
566
        handle->plugin_handle = session;
567

    
568
        return;
569
}
570

    
571
void janus_videocall_destroy_session(janus_plugin_session *handle, int *error) {
572
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized)) {
573
                *error = -1;
574
                return;
575
        }
576
        janus_videocall_session *session = (janus_videocall_session *)handle->plugin_handle; 
577
        if(!session) {
578
                JANUS_LOG(LOG_ERR, "No VideoCall session associated with this handle...\n");
579
                *error = -2;
580
                return;
581
        }
582
        janus_mutex_lock(&sessions_mutex);
583
        if(!session->destroyed) {
584
                JANUS_LOG(LOG_VERB, "Removing VideoCall user %s session...\n", session->username ? session->username : "'unknown'");
585
                janus_videocall_hangup_media(handle);
586
                session->destroyed = janus_get_monotonic_time();
587
                if(session->username != NULL) {
588
                        int res = g_hash_table_remove(sessions, (gpointer)session->username);
589
                        JANUS_LOG(LOG_VERB, "  -- Removed: %d\n", res);
590
                }
591
                /* Cleaning up and removing the session is done in a lazy way */
592
                old_sessions = g_list_append(old_sessions, session);
593
        }
594
        janus_mutex_unlock(&sessions_mutex);
595
        return;
596
}
597

    
598
char *janus_videocall_query_session(janus_plugin_session *handle) {
599
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized)) {
600
                return NULL;
601
        }        
602
        janus_videocall_session *session = (janus_videocall_session *)handle->plugin_handle;
603
        if(!session) {
604
                JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
605
                return NULL;
606
        }
607
        /* Provide some generic info, e.g., if we're in a call and with whom */
608
        json_t *info = json_object();
609
        json_object_set_new(info, "state", json_string(session->peer ? "incall" : "idle"));
610
        json_object_set_new(info, "username", session->username ? json_string(session->username) : NULL);
611
        if(session->peer) {
612
                json_object_set_new(info, "peer", session->peer->username ? json_string(session->peer->username) : NULL);
613
                json_object_set_new(info, "audio_active", json_string(session->audio_active ? "true" : "false"));
614
                json_object_set_new(info, "video_active", json_string(session->video_active ? "true" : "false"));
615
                json_object_set_new(info, "bitrate", json_integer(session->bitrate));
616
                json_object_set_new(info, "slowlink_count", json_integer(session->slowlink_count));
617
        }
618
        if(session->arc || session->vrc) {
619
                json_t *recording = json_object();
620
                if(session->arc && session->arc->filename)
621
                        json_object_set_new(recording, "audio", json_string(session->arc->filename));
622
                if(session->vrc && session->vrc->filename)
623
                        json_object_set_new(recording, "video", json_string(session->vrc->filename));
624
                json_object_set_new(info, "recording", recording);
625
        }
626
        json_object_set_new(info, "destroyed", json_integer(session->destroyed));
627
        char *info_text = json_dumps(info, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
628
        json_decref(info);
629
        return info_text;
630
}
631

    
632
struct janus_plugin_result *janus_videocall_handle_message(janus_plugin_session *handle, char *transaction, char *message, char *sdp_type, char *sdp) {
633
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
634
                return janus_plugin_result_new(JANUS_PLUGIN_ERROR, g_atomic_int_get(&stopping) ? "Shutting down" : "Plugin not initialized");
635
        janus_videocall_message *msg = g_malloc0(sizeof(janus_videocall_message));
636
        if(msg == NULL) {
637
                JANUS_LOG(LOG_FATAL, "Memory error!\n");
638
                return janus_plugin_result_new(JANUS_PLUGIN_ERROR, "Memory error");
639
        }
640
        msg->handle = handle;
641
        msg->transaction = transaction;
642
        msg->message = message;
643
        msg->sdp_type = sdp_type;
644
        msg->sdp = sdp;
645
        g_async_queue_push(messages, msg);
646

    
647
        /* All the requests to this plugin are handled asynchronously */
648
        return janus_plugin_result_new(JANUS_PLUGIN_OK_WAIT, NULL);
649
}
650

    
651
void janus_videocall_setup_media(janus_plugin_session *handle) {
652
        JANUS_LOG(LOG_INFO, "WebRTC media is now available\n");
653
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
654
                return;
655
        janus_videocall_session *session = (janus_videocall_session *)handle->plugin_handle;        
656
        if(!session) {
657
                JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
658
                return;
659
        }
660
        if(session->destroyed)
661
                return;
662
        g_atomic_int_set(&session->hangingup, 0);
663
        /* We really don't care, as we only relay RTP/RTCP we get in the first place anyway */
664
}
665

    
666
void janus_videocall_incoming_rtp(janus_plugin_session *handle, int video, char *buf, int len) {
667
        if(handle == NULL || handle->stopped || g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
668
                return;
669
        if(gateway) {
670
                /* Honour the audio/video active flags */
671
                janus_videocall_session *session = (janus_videocall_session *)handle->plugin_handle;        
672
                if(!session) {
673
                        JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
674
                        return;
675
                }
676
                if(!session->peer) {
677
                        JANUS_LOG(LOG_ERR, "Session has no peer...\n");
678
                        return;
679
                }
680
                if(session->destroyed || session->peer->destroyed)
681
                        return;
682
                if((!video && session->audio_active) || (video && session->video_active)) {
683
                        /* Save the frame if we're recording */
684
                        janus_recorder_save_frame(video ? session->vrc : session->arc, buf, len);
685
                        /* Forward the packet to the peer */
686
                        gateway->relay_rtp(session->peer->handle, video, buf, len);
687
                }
688
        }
689
}
690

    
691
void janus_videocall_incoming_rtcp(janus_plugin_session *handle, int video, char *buf, int len) {
692
        if(handle == NULL || handle->stopped || g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
693
                return;
694
        if(gateway) {
695
                janus_videocall_session *session = (janus_videocall_session *)handle->plugin_handle;        
696
                if(!session) {
697
                        JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
698
                        return;
699
                }
700
                if(!session->peer) {
701
                        JANUS_LOG(LOG_ERR, "Session has no peer...\n");
702
                        return;
703
                }
704
                if(session->destroyed || session->peer->destroyed)
705
                        return;
706
                if(session->bitrate > 0)
707
                        janus_rtcp_cap_remb(buf, len, session->bitrate);
708
                gateway->relay_rtcp(session->peer->handle, video, buf, len);
709
        }
710
}
711

    
712
void janus_videocall_incoming_data(janus_plugin_session *handle, char *buf, int len) {
713
        if(handle == NULL || handle->stopped || g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
714
                return;
715
        if(gateway) {
716
                janus_videocall_session *session = (janus_videocall_session *)handle->plugin_handle;        
717
                if(!session) {
718
                        JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
719
                        return;
720
                }
721
                if(!session->peer) {
722
                        JANUS_LOG(LOG_ERR, "Session has no peer...\n");
723
                        return;
724
                }
725
                if(session->destroyed || session->peer->destroyed)
726
                        return;
727
                if(buf == NULL || len <= 0)
728
                        return;
729
                char *text = g_malloc0(len+1);
730
                memcpy(text, buf, len);
731
                *(text+len) = '\0';
732
                JANUS_LOG(LOG_VERB, "Got a DataChannel message (%zu bytes) to forward: %s\n", strlen(text), text);
733
                gateway->relay_data(session->peer->handle, text, strlen(text));
734
                g_free(text);
735
        }
736
}
737

    
738
void janus_videocall_slow_link(janus_plugin_session *handle, int uplink, int video) {
739
        /* The core is informing us that our peer got or sent too many NACKs, are we pushing media too hard? */
740
        if(handle == NULL || handle->stopped || g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
741
                return;
742
        janus_videocall_session *session = (janus_videocall_session *)handle->plugin_handle;        
743
        if(!session) {
744
                JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
745
                return;
746
        }
747
        if(session->destroyed)
748
                return;
749
        session->slowlink_count++;
750
        if(uplink && !video && !session->audio_active) {
751
                /* We're not relaying audio and the peer is expecting it, so NACKs are normal */
752
                JANUS_LOG(LOG_VERB, "Getting a lot of NACKs (slow uplink) for audio, but that's expected, a configure disabled the audio forwarding\n");
753
        } else if(uplink && video && !session->video_active) {
754
                /* We're not relaying video and the peer is expecting it, so NACKs are normal */
755
                JANUS_LOG(LOG_VERB, "Getting a lot of NACKs (slow uplink) for video, but that's expected, a configure disabled the video forwarding\n");
756
        } else {
757
                /* Slow uplink or downlink, maybe we set the bitrate cap too high? */
758
                if(video) {
759
                        /* Halve the bitrate, but don't go too low... */
760
                        if(!uplink) {
761
                                /* Downlink issue, user has trouble sending, halve this user's bitrate cap */
762
                                session->bitrate = session->bitrate > 0 ? session->bitrate : 512*1024;
763
                                session->bitrate = session->bitrate/2;
764
                                if(session->bitrate < 64*1024)
765
                                        session->bitrate = 64*1024;
766
                        } else {
767
                                /* Uplink issue, user has trouble receiving, halve this user's peer's bitrate cap */
768
                                if(session->peer == NULL || session->peer->handle == NULL)
769
                                        return;        /* Nothing to do */
770
                                session->peer->bitrate = session->peer->bitrate > 0 ? session->peer->bitrate : 512*1024;
771
                                session->peer->bitrate = session->peer->bitrate/2;
772
                                if(session->peer->bitrate < 64*1024)
773
                                        session->peer->bitrate = 64*1024;
774
                        }
775
                        JANUS_LOG(LOG_WARN, "Getting a lot of NACKs (slow %s) for %s, forcing a lower REMB: %"SCNu64"\n",
776
                                uplink ? "uplink" : "downlink", video ? "video" : "audio", uplink ? session->peer->bitrate : session->bitrate);
777
                        /* ... and send a new REMB back */
778
                        char rtcpbuf[24];
779
                        janus_rtcp_remb((char *)(&rtcpbuf), 24, uplink ? session->peer->bitrate : session->bitrate);
780
                        gateway->relay_rtcp(uplink ? session->peer->handle : handle, 1, rtcpbuf, 24);
781
                        /* As a last thing, notify the affected user about this */
782
                        json_t *event = json_object();
783
                        json_object_set_new(event, "videocall", json_string("event"));
784
                        json_t *result = json_object();
785
                        json_object_set_new(result, "status", json_string("slow_link"));
786
                        json_object_set_new(result, "bitrate", json_integer(uplink ? session->peer->bitrate : session->bitrate));
787
                        json_object_set_new(event, "result", result);
788
                        char *event_text = json_dumps(event, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
789
                        json_decref(event);
790
                        event = NULL;
791
                        gateway->push_event(uplink ? session->peer->handle : handle, &janus_videocall_plugin, NULL, event_text, NULL, NULL);
792
                        g_free(event_text);
793
                }
794
        }
795
}
796

    
797
void janus_videocall_hangup_media(janus_plugin_session *handle) {
798
        JANUS_LOG(LOG_INFO, "No WebRTC media anymore\n");
799
        if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
800
                return;
801
        janus_videocall_session *session = (janus_videocall_session *)handle->plugin_handle;        
802
        if(!session) {
803
                JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
804
                return;
805
        }
806
        if(session->destroyed)
807
                return;
808
        if(g_atomic_int_add(&session->hangingup, 1))
809
                return;
810
        /* Get rid of the recorders, if available */
811
        janus_mutex_lock(&session->rec_mutex);
812
        if(session->arc) {
813
                janus_recorder_close(session->arc);
814
                JANUS_LOG(LOG_INFO, "Closed audio recording %s\n", session->arc->filename ? session->arc->filename : "??");
815
                janus_recorder_free(session->arc);
816
        }
817
        session->arc = NULL;
818
        if(session->vrc) {
819
                janus_recorder_close(session->vrc);
820
                JANUS_LOG(LOG_INFO, "Closed video recording %s\n", session->vrc->filename ? session->vrc->filename : "??");
821
                janus_recorder_free(session->vrc);
822
        }
823
        session->vrc = NULL;
824
        janus_mutex_unlock(&session->rec_mutex);
825
        if(session->peer) {
826
                /* Send event to our peer too */
827
                json_t *call = json_object();
828
                json_object_set_new(call, "videocall", json_string("event"));
829
                json_t *calling = json_object();
830
                json_object_set_new(calling, "event", json_string("hangup"));
831
                json_object_set_new(calling, "username", json_string(session->username));
832
                json_object_set_new(calling, "reason", json_string("Remote WebRTC hangup"));
833
                json_object_set_new(call, "result", calling);
834
                char *call_text = json_dumps(call, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
835
                json_decref(call);
836
                JANUS_LOG(LOG_VERB, "Pushing event to peer: %s\n", call_text);
837
                int ret = gateway->push_event(session->peer->handle, &janus_videocall_plugin, NULL, call_text, NULL, NULL);
838
                JANUS_LOG(LOG_VERB, "  >> %d (%s)\n", ret, janus_get_api_error(ret));
839
                g_free(call_text);
840
                /* Also notify event handlers */
841
                if(notify_events && gateway->events_is_enabled()) {
842
                        json_t *info = json_object();
843
                        json_object_set_new(info, "event", json_string("hangup"));
844
                        json_object_set_new(info, "reason", json_string("Remote WebRTC hangup"));
845
                        gateway->notify_event(session->peer->handle, info);
846
                }
847
        }
848
        session->peer = NULL;
849
        /* Reset controls */
850
        session->has_audio = FALSE;
851
        session->has_video = FALSE;
852
        session->audio_active = TRUE;
853
        session->video_active = TRUE;
854
        session->bitrate = 0;
855
}
856

    
857
/* Thread to handle incoming messages */
858
static void *janus_videocall_handler(void *data) {
859
        JANUS_LOG(LOG_VERB, "Joining VideoCall handler thread\n");
860
        janus_videocall_message *msg = NULL;
861
        int error_code = 0;
862
        char error_cause[512];
863
        json_t *root = NULL;
864
        while(g_atomic_int_get(&initialized) && !g_atomic_int_get(&stopping)) {
865
                msg = g_async_queue_pop(messages);
866
                if(msg == NULL)
867
                        continue;
868
                if(msg == &exit_message)
869
                        break;
870
                if(msg->handle == NULL) {
871
                        janus_videocall_message_free(msg);
872
                        continue;
873
                }
874
                janus_videocall_session *session = (janus_videocall_session *)msg->handle->plugin_handle;
875
                if(!session) {
876
                        JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
877
                        janus_videocall_message_free(msg);
878
                        continue;
879
                }
880
                if(session->destroyed) {
881
                        janus_videocall_message_free(msg);
882
                        continue;
883
                }
884
                /* Handle request */
885
                error_code = 0;
886
                root = NULL;
887
                JANUS_LOG(LOG_VERB, "Handling message: %s\n", msg->message);
888
                if(msg->message == NULL) {
889
                        JANUS_LOG(LOG_ERR, "No message??\n");
890
                        error_code = JANUS_VIDEOCALL_ERROR_NO_MESSAGE;
891
                        g_snprintf(error_cause, 512, "%s", "No message??");
892
                        goto error;
893
                }
894
                json_error_t error;
895
                root = json_loads(msg->message, 0, &error);
896
                if(!root) {
897
                        JANUS_LOG(LOG_ERR, "JSON error: on line %d: %s\n", error.line, error.text);
898
                        error_code = JANUS_VIDEOCALL_ERROR_INVALID_JSON;
899
                        g_snprintf(error_cause, 512, "JSON error: on line %d: %s", error.line, error.text);
900
                        goto error;
901
                }
902
                if(!json_is_object(root)) {
903
                        JANUS_LOG(LOG_ERR, "JSON error: not an object\n");
904
                        error_code = JANUS_VIDEOCALL_ERROR_INVALID_JSON;
905
                        g_snprintf(error_cause, 512, "JSON error: not an object");
906
                        goto error;
907
                }
908
                JANUS_VALIDATE_JSON_OBJECT(root, request_parameters,
909
                        error_code, error_cause, TRUE,
910
                        JANUS_VIDEOCALL_ERROR_MISSING_ELEMENT, JANUS_VIDEOCALL_ERROR_INVALID_ELEMENT);
911
                if(error_code != 0)
912
                        goto error;
913
                json_t *request = json_object_get(root, "request");
914
                const char *request_text = json_string_value(request);
915
                json_t *result = NULL;
916
                char *sdp_type = NULL, *sdp = NULL;
917
                if(!strcasecmp(request_text, "list")) {
918
                        result = json_object();
919
                        json_t *list = json_array();
920
                        JANUS_LOG(LOG_VERB, "Request for the list of peers\n");
921
                        /* Return a list of all available mountpoints */
922
                        janus_mutex_lock(&sessions_mutex);
923
                        GHashTableIter iter;
924
                        gpointer value;
925
                        g_hash_table_iter_init(&iter, sessions);
926
                        while (g_hash_table_iter_next(&iter, NULL, &value)) {
927
                                janus_videocall_session *user = value;
928
                                if(user != NULL && user->username != NULL)
929
                                        json_array_append_new(list, json_string(user->username));
930
                        }
931
                        json_object_set_new(result, "list", list);
932
                        janus_mutex_unlock(&sessions_mutex);
933
                } else if(!strcasecmp(request_text, "register")) {
934
                        /* Map this handle to a username */
935
                        if(session->username != NULL) {
936
                                JANUS_LOG(LOG_ERR, "Already registered (%s)\n", session->username);
937
                                error_code = JANUS_VIDEOCALL_ERROR_ALREADY_REGISTERED;
938
                                g_snprintf(error_cause, 512, "Already registered (%s)", session->username);
939
                                goto error;
940
                        }
941
                        JANUS_VALIDATE_JSON_OBJECT(root, username_parameters,
942
                                error_code, error_cause, TRUE,
943
                                JANUS_VIDEOCALL_ERROR_MISSING_ELEMENT, JANUS_VIDEOCALL_ERROR_INVALID_ELEMENT);
944
                        if(error_code != 0)
945
                                goto error;
946
                        json_t *username = json_object_get(root, "username");
947
                        const char *username_text = json_string_value(username);
948
                        janus_mutex_lock(&sessions_mutex);
949
                        if(g_hash_table_lookup(sessions, username_text) != NULL) {
950
                                janus_mutex_unlock(&sessions_mutex);
951
                                JANUS_LOG(LOG_ERR, "Username '%s' already taken\n", username_text);
952
                                error_code = JANUS_VIDEOCALL_ERROR_USERNAME_TAKEN;
953
                                g_snprintf(error_cause, 512, "Username '%s' already taken", username_text);
954
                                goto error;
955
                        }
956
                        janus_mutex_unlock(&sessions_mutex);
957
                        session->username = g_strdup(username_text);
958
                        if(session->username == NULL) {
959
                                JANUS_LOG(LOG_FATAL, "Memory error!\n");
960
                                error_code = JANUS_VIDEOCALL_ERROR_UNKNOWN_ERROR;
961
                                g_snprintf(error_cause, 512, "Memory error");
962
                                goto error;
963
                        }
964
                        janus_mutex_lock(&sessions_mutex);
965
                        g_hash_table_insert(sessions, (gpointer)session->username, session);
966
                        janus_mutex_unlock(&sessions_mutex);
967
                        result = json_object();
968
                        json_object_set_new(result, "event", json_string("registered"));
969
                        json_object_set_new(result, "username", json_string(username_text));
970
                        /* Also notify event handlers */
971
                        if(notify_events && gateway->events_is_enabled()) {
972
                                json_t *info = json_object();
973
                                json_object_set_new(info, "event", json_string("registered"));
974
                                json_object_set_new(info, "username", json_string(username_text));
975
                                gateway->notify_event(session->peer->handle, info);
976
                        }
977
                } else if(!strcasecmp(request_text, "call")) {
978
                        /* Call another peer */
979
                        if(session->username == NULL) {
980
                                JANUS_LOG(LOG_ERR, "Register a username first\n");
981
                                error_code = JANUS_VIDEOCALL_ERROR_REGISTER_FIRST;
982
                                g_snprintf(error_cause, 512, "Register a username first");
983
                                goto error;
984
                        }
985
                        if(session->peer != NULL) {
986
                                JANUS_LOG(LOG_ERR, "Already in a call\n");
987
                                error_code = JANUS_VIDEOCALL_ERROR_ALREADY_IN_CALL;
988
                                g_snprintf(error_cause, 512, "Already in a call");
989
                                goto error;
990
                        }
991
                        JANUS_VALIDATE_JSON_OBJECT(root, username_parameters,
992
                                error_code, error_cause, TRUE,
993
                                JANUS_VIDEOCALL_ERROR_MISSING_ELEMENT, JANUS_VIDEOCALL_ERROR_INVALID_ELEMENT);
994
                        if(error_code != 0)
995
                                goto error;
996
                        json_t *username = json_object_get(root, "username");
997
                        const char *username_text = json_string_value(username);
998
                        if(!strcmp(username_text, session->username)) {
999
                                JANUS_LOG(LOG_ERR, "You can't call yourself... use the EchoTest for that\n");
1000
                                error_code = JANUS_VIDEOCALL_ERROR_USE_ECHO_TEST;
1001
                                g_snprintf(error_cause, 512, "You can't call yourself... use the EchoTest for that");
1002
                                goto error;
1003
                        }
1004
                        janus_mutex_lock(&sessions_mutex);
1005
                        janus_videocall_session *peer = g_hash_table_lookup(sessions, username_text);
1006
                        if(peer == NULL || peer->destroyed) {
1007
                                janus_mutex_unlock(&sessions_mutex);
1008
                                JANUS_LOG(LOG_ERR, "Username '%s' doesn't exist\n", username_text);
1009
                                error_code = JANUS_VIDEOCALL_ERROR_NO_SUCH_USERNAME;
1010
                                g_snprintf(error_cause, 512, "Username '%s' doesn't exist", username_text);
1011
                                goto error;
1012
                        }
1013
                        if(peer->peer != NULL) {
1014
                                janus_mutex_unlock(&sessions_mutex);
1015
                                JANUS_LOG(LOG_VERB, "%s is busy\n", username_text);
1016
                                result = json_object();
1017
                                json_object_set_new(result, "event", json_string("hangup"));
1018
                                json_object_set_new(result, "username", json_string(session->username));
1019
                                json_object_set_new(result, "reason", json_string("User busy"));
1020
                                /* Also notify event handlers */
1021
                                if(notify_events && gateway->events_is_enabled()) {
1022
                                        json_t *info = json_object();
1023
                                        json_object_set_new(info, "event", json_string("hangup"));
1024
                                        json_object_set_new(info, "reason", json_string("User busy"));
1025
                                        gateway->notify_event(session->handle, info);
1026
                                }
1027
                        } else {
1028
                                janus_mutex_unlock(&sessions_mutex);
1029
                                /* Any SDP to handle? if not, something's wrong */
1030
                                if(!msg->sdp) {
1031
                                        JANUS_LOG(LOG_ERR, "Missing SDP\n");
1032
                                        error_code = JANUS_VIDEOCALL_ERROR_MISSING_SDP;
1033
                                        g_snprintf(error_cause, 512, "Missing SDP");
1034
                                        goto error;
1035
                                }
1036
                                janus_mutex_lock(&sessions_mutex);
1037
                                session->peer = peer;
1038
                                peer->peer = session;
1039
                                session->has_audio = (strstr(msg->sdp, "m=audio") != NULL);
1040
                                session->has_video = (strstr(msg->sdp, "m=video") != NULL);
1041
                                janus_mutex_unlock(&sessions_mutex);
1042
                                JANUS_LOG(LOG_VERB, "%s is calling %s\n", session->username, session->peer->username);
1043
                                JANUS_LOG(LOG_VERB, "This is involving a negotiation (%s) as well:\n%s\n", msg->sdp_type, msg->sdp);
1044
                                /* Send SDP to our peer */
1045
                                json_t *call = json_object();
1046
                                json_object_set_new(call, "videocall", json_string("event"));
1047
                                json_t *calling = json_object();
1048
                                json_object_set_new(calling, "event", json_string("incomingcall"));
1049
                                json_object_set_new(calling, "username", json_string(session->username));
1050
                                json_object_set_new(call, "result", calling);
1051
                                char *call_text = json_dumps(call, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1052
                                json_decref(call);
1053
                                /* Make also sure we get rid of ULPfec, red, etc. */
1054
                                char *sdp = g_strdup(msg->sdp);
1055
                                if(strstr(sdp, "ulpfec")) {
1056
                                        /* FIXME This really needs some better code */
1057
                                        sdp = janus_string_replace(sdp, "a=rtpmap:116 red/90000\r\n", "");
1058
                                        sdp = janus_string_replace(sdp, "a=rtpmap:117 ulpfec/90000\r\n", "");
1059
                                        sdp = janus_string_replace(sdp, "a=rtpmap:96 rtx/90000\r\n", "");
1060
                                        sdp = janus_string_replace(sdp, "a=fmtp:96 apt=100\r\n", "");
1061
                                        sdp = janus_string_replace(sdp, "a=rtpmap:97 rtx/90000\r\n", "");
1062
                                        sdp = janus_string_replace(sdp, "a=fmtp:97 apt=101\r\n", "");
1063
                                        sdp = janus_string_replace(sdp, "a=rtpmap:98 rtx/90000\r\n", "");
1064
                                        sdp = janus_string_replace(sdp, "a=fmtp:98 apt=116\r\n", "");
1065
                                        sdp = janus_string_replace(sdp, " 116", "");
1066
                                        sdp = janus_string_replace(sdp, " 117", "");
1067
                                        sdp = janus_string_replace(sdp, " 96", "");
1068
                                        sdp = janus_string_replace(sdp, " 97", "");
1069
                                        sdp = janus_string_replace(sdp, " 98", "");
1070
                                }
1071
                                g_atomic_int_set(&session->hangingup, 0);
1072
                                JANUS_LOG(LOG_VERB, "Pushing event to peer: %s\n", call_text);
1073
                                int ret = gateway->push_event(peer->handle, &janus_videocall_plugin, NULL, call_text, msg->sdp_type, sdp);
1074
                                g_free(sdp);
1075
                                JANUS_LOG(LOG_VERB, "  >> %d (%s)\n", ret, janus_get_api_error(ret));
1076
                                g_free(call_text);
1077
                                /* Send an ack back */
1078
                                result = json_object();
1079
                                json_object_set_new(result, "event", json_string("calling"));
1080
                                /* Also notify event handlers */
1081
                                if(notify_events && gateway->events_is_enabled()) {
1082
                                        json_t *info = json_object();
1083
                                        json_object_set_new(info, "event", json_string("calling"));
1084
                                        gateway->notify_event(session->handle, info);
1085
                                }
1086
                        }
1087
                } else if(!strcasecmp(request_text, "accept")) {
1088
                        /* Accept a call from another peer */
1089
                        if(session->peer == NULL) {
1090
                                JANUS_LOG(LOG_ERR, "No incoming call to accept\n");
1091
                                error_code = JANUS_VIDEOCALL_ERROR_NO_CALL;
1092
                                g_snprintf(error_cause, 512, "No incoming call to accept");
1093
                                goto error;
1094
                        }
1095
                        /* Any SDP to handle? if not, something's wrong */
1096
                        if(!msg->sdp) {
1097
                                JANUS_LOG(LOG_ERR, "Missing SDP\n");
1098
                                error_code = JANUS_VIDEOCALL_ERROR_MISSING_SDP;
1099
                                g_snprintf(error_cause, 512, "Missing SDP");
1100
                                goto error;
1101
                        }
1102
                        JANUS_LOG(LOG_VERB, "%s is accepting a call from %s\n", session->username, session->peer->username);
1103
                        JANUS_LOG(LOG_VERB, "This is involving a negotiation (%s) as well:\n%s\n", msg->sdp_type, msg->sdp);
1104
                        session->has_audio = (strstr(msg->sdp, "m=audio") != NULL);
1105
                        session->has_video = (strstr(msg->sdp, "m=video") != NULL);
1106
                        /* Send SDP to our peer */
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("accepted"));
1111
                        json_object_set_new(calling, "username", json_string(session->username));
1112
                        json_object_set_new(call, "result", calling);
1113
                        char *call_text = json_dumps(call, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1114
                        json_decref(call);
1115
                        g_atomic_int_set(&session->hangingup, 0);
1116
                        JANUS_LOG(LOG_VERB, "Pushing event to peer: %s\n", call_text);
1117
                        int ret = gateway->push_event(session->peer->handle, &janus_videocall_plugin, NULL, call_text, msg->sdp_type, msg->sdp);
1118
                        JANUS_LOG(LOG_VERB, "  >> %d (%s)\n", ret, janus_get_api_error(ret));
1119
                        g_free(call_text);
1120
                        /* Send an ack back */
1121
                        result = json_object();
1122
                        json_object_set_new(result, "event", json_string("accepted"));
1123
                        /* Also notify event handlers */
1124
                        if(notify_events && gateway->events_is_enabled()) {
1125
                                json_t *info = json_object();
1126
                                json_object_set_new(info, "event", json_string("accepted"));
1127
                                gateway->notify_event(session->handle, info);
1128
                        }
1129
                } else if(!strcasecmp(request_text, "set")) {
1130
                        /* Update the local configuration (audio/video mute/unmute, bitrate cap or recording) */
1131
                        JANUS_VALIDATE_JSON_OBJECT(root, set_parameters,
1132
                                error_code, error_cause, TRUE,
1133
                                JANUS_VIDEOCALL_ERROR_MISSING_ELEMENT, JANUS_VIDEOCALL_ERROR_INVALID_ELEMENT);
1134
                        if(error_code != 0)
1135
                                goto error;
1136
                        json_t *audio = json_object_get(root, "audio");
1137
                        json_t *video = json_object_get(root, "video");
1138
                        json_t *bitrate = json_object_get(root, "bitrate");
1139
                        json_t *record = json_object_get(root, "record");
1140
                        json_t *recfile = json_object_get(root, "filename");
1141
                        if(audio) {
1142
                                session->audio_active = json_is_true(audio);
1143
                                JANUS_LOG(LOG_VERB, "Setting audio property: %s\n", session->audio_active ? "true" : "false");
1144
                        }
1145
                        if(video) {
1146
                                if(!session->video_active && json_is_true(video)) {
1147
                                        /* Send a PLI */
1148
                                        JANUS_LOG(LOG_VERB, "Just (re-)enabled video, sending a PLI to recover it\n");
1149
                                        char buf[12];
1150
                                        memset(buf, 0, 12);
1151
                                        janus_rtcp_pli((char *)&buf, 12);
1152
                                        gateway->relay_rtcp(session->handle, 1, buf, 12);
1153
                                }
1154
                                session->video_active = json_is_true(video);
1155
                                JANUS_LOG(LOG_VERB, "Setting video property: %s\n", session->video_active ? "true" : "false");
1156
                        }
1157
                        if(bitrate) {
1158
                                session->bitrate = json_integer_value(bitrate);
1159
                                JANUS_LOG(LOG_VERB, "Setting video bitrate: %"SCNu64"\n", session->bitrate);
1160
                                if(session->bitrate > 0) {
1161
                                        /* FIXME Generate a new REMB (especially useful for Firefox, which doesn't send any we can cap later) */
1162
                                        char buf[24];
1163
                                        memset(buf, 0, 24);
1164
                                        janus_rtcp_remb((char *)&buf, 24, session->bitrate);
1165
                                        JANUS_LOG(LOG_VERB, "Sending REMB\n");
1166
                                        gateway->relay_rtcp(session->handle, 1, buf, 24);
1167
                                        /* FIXME How should we handle a subsequent "no limit" bitrate? */
1168
                                }
1169
                        }
1170
                        if(record) {
1171
                                if(msg->sdp) {
1172
                                        session->has_audio = (strstr(msg->sdp, "m=audio") != NULL);
1173
                                        session->has_video = (strstr(msg->sdp, "m=video") != NULL);
1174
                                }
1175
                                gboolean recording = json_is_true(record);
1176
                                const char *recording_base = json_string_value(recfile);
1177
                                JANUS_LOG(LOG_VERB, "Recording %s (base filename: %s)\n", recording ? "enabled" : "disabled", recording_base ? recording_base : "not provided");
1178
                                janus_mutex_lock(&session->rec_mutex);
1179
                                if(!recording) {
1180
                                        /* Not recording (anymore?) */
1181
                                        if(session->arc) {
1182
                                                janus_recorder_close(session->arc);
1183
                                                JANUS_LOG(LOG_INFO, "Closed audio recording %s\n", session->arc->filename ? session->arc->filename : "??");
1184
                                                janus_recorder_free(session->arc);
1185
                                        }
1186
                                        session->arc = NULL;
1187
                                        if(session->vrc) {
1188
                                                janus_recorder_close(session->vrc);
1189
                                                JANUS_LOG(LOG_INFO, "Closed video recording %s\n", session->vrc->filename ? session->vrc->filename : "??");
1190
                                                janus_recorder_free(session->vrc);
1191
                                        }
1192
                                        session->vrc = NULL;
1193
                                } else {
1194
                                        /* We've started recording, send a PLI and go on */
1195
                                        char filename[255];
1196
                                        gint64 now = janus_get_real_time();
1197
                                        if(session->has_audio) {
1198
                                                /* FIXME We assume we're recording Opus, here */
1199
                                                memset(filename, 0, 255);
1200
                                                if(recording_base) {
1201
                                                        /* Use the filename and path we have been provided */
1202
                                                        g_snprintf(filename, 255, "%s-audio", recording_base);
1203
                                                        session->arc = janus_recorder_create(NULL, "opus", filename);
1204
                                                        if(session->arc == NULL) {
1205
                                                                /* FIXME We should notify the fact the recorder could not be created */
1206
                                                                JANUS_LOG(LOG_ERR, "Couldn't open an audio recording file for this VideoCall user!\n");
1207
                                                        }
1208
                                                } else {
1209
                                                        /* Build a filename */
1210
                                                        g_snprintf(filename, 255, "videocall-%s-%s-%"SCNi64"-audio",
1211
                                                                session->username ? session->username : "unknown",
1212
                                                                (session->peer && session->peer->username) ? session->peer->username : "unknown",
1213
                                                                now);
1214
                                                        session->arc = janus_recorder_create(NULL, "opus", filename);
1215
                                                        if(session->arc == NULL) {
1216
                                                                /* FIXME We should notify the fact the recorder could not be created */
1217
                                                                JANUS_LOG(LOG_ERR, "Couldn't open an audio recording file for this VideoCall user!\n");
1218
                                                        }
1219
                                                }
1220
                                        }
1221
                                        if(session->has_video) {
1222
                                                /* FIXME We assume we're recording VP8, here */
1223
                                                memset(filename, 0, 255);
1224
                                                if(recording_base) {
1225
                                                        /* Use the filename and path we have been provided */
1226
                                                        g_snprintf(filename, 255, "%s-video", recording_base);
1227
                                                        session->vrc = janus_recorder_create(NULL, "vp8", filename);
1228
                                                        if(session->vrc == NULL) {
1229
                                                                /* FIXME We should notify the fact the recorder could not be created */
1230
                                                                JANUS_LOG(LOG_ERR, "Couldn't open an video recording file for this VideoCall user!\n");
1231
                                                        }
1232
                                                } else {
1233
                                                        /* Build a filename */
1234
                                                        g_snprintf(filename, 255, "videocall-%s-%s-%"SCNi64"-video",
1235
                                                                session->username ? session->username : "unknown",
1236
                                                                (session->peer && session->peer->username) ? session->peer->username : "unknown",
1237
                                                                now);
1238
                                                        session->vrc = janus_recorder_create(NULL, "vp8", filename);
1239
                                                        if(session->vrc == NULL) {
1240
                                                                /* FIXME We should notify the fact the recorder could not be created */
1241
                                                                JANUS_LOG(LOG_ERR, "Couldn't open an video recording file for this VideoCall user!\n");
1242
                                                        }
1243
                                                }
1244
                                                /* Send a PLI */
1245
                                                JANUS_LOG(LOG_VERB, "Recording video, sending a PLI to kickstart it\n");
1246
                                                char buf[12];
1247
                                                memset(buf, 0, 12);
1248
                                                janus_rtcp_pli((char *)&buf, 12);
1249
                                                gateway->relay_rtcp(session->handle, 1, buf, 12);
1250
                                        }
1251
                                }
1252
                                janus_mutex_unlock(&session->rec_mutex);
1253
                        }
1254
                        /* Also notify event handlers */
1255
                        if(notify_events && gateway->events_is_enabled()) {
1256
                                json_t *info = json_object();
1257
                                json_object_set_new(info, "event", json_string("configured"));
1258
                                json_object_set_new(info, "audio_active", session->audio_active ? json_true() : json_false());
1259
                                json_object_set_new(info, "video_active", session->video_active ? json_true() : json_false());
1260
                                json_object_set_new(info, "bitrate", json_integer(session->bitrate));
1261
                                if(session->arc || session->vrc) {
1262
                                        json_t *recording = json_object();
1263
                                        if(session->arc && session->arc->filename)
1264
                                                json_object_set_new(recording, "audio", json_string(session->arc->filename));
1265
                                        if(session->vrc && session->vrc->filename)
1266
                                                json_object_set_new(recording, "video", json_string(session->vrc->filename));
1267
                                        json_object_set_new(info, "recording", recording);
1268
                                }
1269
                                gateway->notify_event(session->handle, info);
1270
                        }
1271
                        /* Send an ack back */
1272
                        result = json_object();
1273
                        json_object_set_new(result, "event", json_string("set"));
1274
                } else if(!strcasecmp(request_text, "hangup")) {
1275
                        /* Hangup an ongoing call or reject an incoming one */
1276
                        janus_mutex_lock(&sessions_mutex);
1277
                        janus_videocall_session *peer = session->peer;
1278
                        if(peer == NULL) {
1279
                                JANUS_LOG(LOG_WARN, "No call to hangup\n");
1280
                        } else {
1281
                                JANUS_LOG(LOG_VERB, "%s is hanging up the call with %s\n", session->username, peer->username);
1282
                                session->peer = NULL;
1283
                                peer->peer = NULL;
1284
                        }
1285
                        janus_mutex_unlock(&sessions_mutex);
1286
                        /* Notify the success as an hangup message */
1287
                        result = json_object();
1288
                        json_object_set_new(result, "event", json_string("hangup"));
1289
                        json_object_set_new(result, "username", json_string(session->username));
1290
                        json_object_set_new(result, "reason", json_string("Explicit hangup"));
1291
                        /* Also notify event handlers */
1292
                        if(notify_events && gateway->events_is_enabled()) {
1293
                                json_t *info = json_object();
1294
                                json_object_set_new(info, "event", json_string("hangup"));
1295
                                json_object_set_new(info, "reason", json_string("Explicit hangup"));
1296
                                gateway->notify_event(session->handle, info);
1297
                        }
1298
                        if(peer != NULL) {
1299
                                /* Send event to our peer too */
1300
                                json_t *call = json_object();
1301
                                json_object_set_new(call, "videocall", json_string("event"));
1302
                                json_t *calling = json_object();
1303
                                json_object_set_new(calling, "event", json_string("hangup"));
1304
                                json_object_set_new(calling, "username", json_string(session->username));
1305
                                json_object_set_new(calling, "reason", json_string("Remote hangup"));
1306
                                json_object_set_new(call, "result", calling);
1307
                                char *call_text = json_dumps(call, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1308
                                json_decref(call);
1309
                                JANUS_LOG(LOG_VERB, "Pushing event to peer: %s\n", call_text);
1310
                                int ret = gateway->push_event(peer->handle, &janus_videocall_plugin, NULL, call_text, NULL, NULL);
1311
                                JANUS_LOG(LOG_VERB, "  >> %d (%s)\n", ret, janus_get_api_error(ret));
1312
                                g_free(call_text);
1313
                                /* Also notify event handlers */
1314
                                if(notify_events && gateway->events_is_enabled()) {
1315
                                        json_t *info = json_object();
1316
                                        json_object_set_new(info, "event", json_string("hangup"));
1317
                                        json_object_set_new(info, "reason", json_string("Remote hangup"));
1318
                                        gateway->notify_event(session->peer->handle, info);
1319
                                }
1320
                        }
1321
                } else {
1322
                        JANUS_LOG(LOG_ERR, "Unknown request (%s)\n", request_text);
1323
                        error_code = JANUS_VIDEOCALL_ERROR_INVALID_REQUEST;
1324
                        g_snprintf(error_cause, 512, "Unknown request (%s)", request_text);
1325
                        goto error;
1326
                }
1327

    
1328
                json_decref(root);
1329
                /* Prepare JSON event */
1330
                json_t *event = json_object();
1331
                json_object_set_new(event, "videocall", json_string("event"));
1332
                if(result != NULL)
1333
                        json_object_set_new(event, "result", result);
1334
                char *event_text = json_dumps(event, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1335
                json_decref(event);
1336
                JANUS_LOG(LOG_VERB, "Pushing event: %s\n", event_text);
1337
                int ret = gateway->push_event(msg->handle, &janus_videocall_plugin, msg->transaction, event_text, sdp_type, sdp);
1338
                JANUS_LOG(LOG_VERB, "  >> %d (%s)\n", ret, janus_get_api_error(ret));
1339
                g_free(event_text);
1340
                if(sdp)
1341
                        g_free(sdp);
1342
                janus_videocall_message_free(msg);
1343
                continue;
1344
                
1345
error:
1346
                {
1347
                        if(root != NULL)
1348
                                json_decref(root);
1349
                        /* Prepare JSON error event */
1350
                        json_t *event = json_object();
1351
                        json_object_set_new(event, "videocall", json_string("event"));
1352
                        json_object_set_new(event, "error_code", json_integer(error_code));
1353
                        json_object_set_new(event, "error", json_string(error_cause));
1354
                        char *event_text = json_dumps(event, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1355
                        json_decref(event);
1356
                        JANUS_LOG(LOG_VERB, "Pushing event: %s\n", event_text);
1357
                        int ret = gateway->push_event(msg->handle, &janus_videocall_plugin, msg->transaction, event_text, NULL, NULL);
1358
                        JANUS_LOG(LOG_VERB, "  >> %d (%s)\n", ret, janus_get_api_error(ret));
1359
                        g_free(event_text);
1360
                        janus_videocall_message_free(msg);
1361
                }
1362
        }
1363
        JANUS_LOG(LOG_VERB, "Leaving VideoCall handler thread\n");
1364
        return NULL;
1365
}