/
janus_textroom.c
1485 lines (1414 loc) · 56.2 KB
/
janus_textroom.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! \file janus_textroom.c
* \author Lorenzo Miniero <lorenzo@meetecho.com>
* \copyright GNU General Public License v3
* \brief Janus TextRoom plugin
* \details This is a plugin implementing a DataChannel only text room.
* As such, it does NOT support or negotiate audio or video, but only
* data channels, in order to provide text broadcasting features. The
* plugin allows users to join multiple text-only rooms via a single
* PeerConnection. Users can send messages either to a room in general
* (broadcasting), or to individual users (whispers). This plugin can be
* used within the context of any application that needs real-time text
* broadcasting (e.g., chatrooms, but not only).
*
* The only message that is sent to the plugin through the Janus API is
* a "setup" message, by which the user initializes the PeerConnection
* itself. Apart from that, all other messages are exchanged directly
* via Data Channels.
*
* Each room can also be configured with an HTTP backend to contact for
* incoming messages. If configured, messages addressed to that room will
* also be forwarded, by means of an HTTP POST, to the specified address.
* Notice that this will only work if libcurl was available when
* configuring and installing Janus.
*
* \note This plugin is only meant to showcase what you can do with
* data channels involving multiple participants at the same time. While
* functional, it's not inherently better or faster than doing the same
* thing using the Janus API messaging itself (e.g., as part of the
* plugin API messaging) or using existing instant messaging protocols
* (e.g., Jabber). In fact, while data channels are being used, you're
* still going through a server, so it's not really peer-to-peer. That
* said, the plugin can be useful if you don't plan to use any other
* infrastructure than Janus, and yet you also want to have text-based
* communication (e.g., to add a chatroom to an audio or video conference).
*
* Notice that, in general, all users can create rooms. If you want to
* limit this functionality, you can configure an admin \c admin_key in
* the plugin settings. When configured, only "create" requests that
* include the correct \c admin_key value in an "admin_key" property
* will succeed, and will be rejected otherwise.
*
* \section textroomapi Text Room API
* TBD.
*
* \ingroup plugins
* \ref plugins
*/
#include "plugins/plugin.h"
#include <jansson.h>
#ifdef HAVE_LIBCURL
#include <curl/curl.h>
#endif
#include "debug.h"
#include "apierror.h"
#include "config.h"
#include "mutex.h"
#include "utils.h"
/* Plugin information */
#define JANUS_TEXTROOM_VERSION 2
#define JANUS_TEXTROOM_VERSION_STRING "0.0.2"
#define JANUS_TEXTROOM_DESCRIPTION "This is a plugin implementing a text-only room for Janus, using DataChannels."
#define JANUS_TEXTROOM_NAME "JANUS TextRoom plugin"
#define JANUS_TEXTROOM_AUTHOR "Meetecho s.r.l."
#define JANUS_TEXTROOM_PACKAGE "janus.plugin.textroom"
/* Plugin methods */
janus_plugin *create(void);
int janus_textroom_init(janus_callbacks *callback, const char *config_path);
void janus_textroom_destroy(void);
int janus_textroom_get_api_compatibility(void);
int janus_textroom_get_version(void);
const char *janus_textroom_get_version_string(void);
const char *janus_textroom_get_description(void);
const char *janus_textroom_get_name(void);
const char *janus_textroom_get_author(void);
const char *janus_textroom_get_package(void);
void janus_textroom_create_session(janus_plugin_session *handle, int *error);
struct janus_plugin_result *janus_textroom_handle_message(janus_plugin_session *handle, char *transaction, json_t *message, json_t *jsep);
void janus_textroom_setup_media(janus_plugin_session *handle);
void janus_textroom_incoming_rtp(janus_plugin_session *handle, int video, char *buf, int len);
void janus_textroom_incoming_rtcp(janus_plugin_session *handle, int video, char *buf, int len);
void janus_textroom_incoming_data(janus_plugin_session *handle, char *buf, int len);
void janus_textroom_slow_link(janus_plugin_session *handle, int uplink, int video);
void janus_textroom_hangup_media(janus_plugin_session *handle);
void janus_textroom_destroy_session(janus_plugin_session *handle, int *error);
json_t *janus_textroom_query_session(janus_plugin_session *handle);
/* Plugin setup */
static janus_plugin janus_textroom_plugin =
JANUS_PLUGIN_INIT (
.init = janus_textroom_init,
.destroy = janus_textroom_destroy,
.get_api_compatibility = janus_textroom_get_api_compatibility,
.get_version = janus_textroom_get_version,
.get_version_string = janus_textroom_get_version_string,
.get_description = janus_textroom_get_description,
.get_name = janus_textroom_get_name,
.get_author = janus_textroom_get_author,
.get_package = janus_textroom_get_package,
.create_session = janus_textroom_create_session,
.handle_message = janus_textroom_handle_message,
.setup_media = janus_textroom_setup_media,
.incoming_rtp = janus_textroom_incoming_rtp,
.incoming_rtcp = janus_textroom_incoming_rtcp,
.incoming_data = janus_textroom_incoming_data,
.slow_link = janus_textroom_slow_link,
.hangup_media = janus_textroom_hangup_media,
.destroy_session = janus_textroom_destroy_session,
.query_session = janus_textroom_query_session,
);
/* Plugin creator */
janus_plugin *create(void) {
JANUS_LOG(LOG_VERB, "%s created!\n", JANUS_TEXTROOM_NAME);
return &janus_textroom_plugin;
}
/* Parameter validation */
static struct janus_json_parameter request_parameters[] = {
{"request", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter transaction_parameters[] = {
{"textroom", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"transaction", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter room_parameters[] = {
{"room", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE}
};
static struct janus_json_parameter adminkey_parameters[] = {
{"admin_key", JSON_STRING, JANUS_JSON_PARAM_REQUIRED}
};
static struct janus_json_parameter create_parameters[] = {
{"description", JSON_STRING, 0},
{"secret", JSON_STRING, 0},
{"pin", JSON_STRING, 0},
{"post", JSON_STRING, 0},
{"is_private", JANUS_JSON_BOOL, 0}
};
static struct janus_json_parameter join_parameters[] = {
{"room", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"username", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"display", JSON_STRING, 0}
};
static struct janus_json_parameter message_parameters[] = {
{"room", JSON_INTEGER, JANUS_JSON_PARAM_REQUIRED | JANUS_JSON_PARAM_POSITIVE},
{"text", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"to", JSON_STRING, 0},
{"tos", JSON_ARRAY, 0},
{"ack", JANUS_JSON_BOOL, 0}
};
/* Static configuration instance */
static janus_config *config = NULL;
static const char *config_folder = NULL;
static janus_mutex config_mutex;
/* Useful stuff */
static volatile gint initialized = 0, stopping = 0;
static janus_callbacks *gateway = NULL;
static GThread *handler_thread;
static GThread *watchdog;
static void *janus_textroom_handler(void *data);
/* JSON serialization options */
static size_t json_format = JSON_INDENT(3) | JSON_PRESERVE_ORDER;
typedef struct janus_textroom_message {
janus_plugin_session *handle;
char *transaction;
json_t *message;
json_t *jsep;
} janus_textroom_message;
static GAsyncQueue *messages = NULL;
static janus_textroom_message exit_message;
static void janus_textroom_message_free(janus_textroom_message *msg) {
if(!msg || msg == &exit_message)
return;
msg->handle = NULL;
g_free(msg->transaction);
msg->transaction = NULL;
if(msg->message)
json_decref(msg->message);
msg->message = NULL;
if(msg->jsep)
json_decref(msg->jsep);
msg->jsep = NULL;
g_free(msg);
}
typedef struct janus_textroom_room {
guint64 room_id; /* Unique room ID */
gchar *room_name; /* Room description */
gchar *room_secret; /* Secret needed to manipulate (e.g., destroy) this room */
gchar *room_pin; /* Password needed to join this room, if any */
gboolean is_private; /* Whether this room is 'private' (as in hidden) or not */
gchar *http_backend; /* Server to contact via HTTP POST for incoming messages, if any */
GHashTable *participants; /* Map of participants */
gint64 destroyed; /* When this room has been destroyed */
janus_mutex mutex; /* Mutex to lock this room instance */
} janus_textroom_room;
static GHashTable *rooms;
static janus_mutex rooms_mutex;
static GList *old_rooms;
static char *admin_key = NULL;
typedef struct janus_textroom_session {
janus_plugin_session *handle;
GHashTable *rooms; /* Map of rooms this user is in, and related participant instance */
janus_mutex mutex; /* Mutex to lock this session */
volatile gint setup;
volatile gint hangingup;
gint64 destroyed; /* Time at which this session was marked as destroyed */
} janus_textroom_session;
static GHashTable *sessions;
static GList *old_sessions;
static janus_mutex sessions_mutex;
typedef struct janus_textroom_participant {
janus_textroom_session *session;
janus_textroom_room *room; /* Room this participant is in */
gchar *username; /* Unique username in the room */
gchar *display; /* Display name in the room, if any */
janus_mutex mutex; /* Mutex to lock this session */
gint64 destroyed; /* When this participant was destroyed */
} janus_textroom_participant;
/* SDP template: we only offer data channels */
#define sdp_template \
"v=0\r\n" \
"o=- %"SCNu64" %"SCNu64" IN IP4 127.0.0.1\r\n" /* We need current time here */ \
"s=Janus TextRoom plugin\r\n" \
"t=0 0\r\n" \
"m=application 1 DTLS/SCTP 5000\r\n" \
"c=IN IP4 1.1.1.1\r\n" \
"a=sctpmap:5000 webrtc-datachannel 16\r\n"
/* Error codes */
#define JANUS_TEXTROOM_ERROR_NO_MESSAGE 411
#define JANUS_TEXTROOM_ERROR_INVALID_JSON 412
#define JANUS_TEXTROOM_ERROR_MISSING_ELEMENT 413
#define JANUS_TEXTROOM_ERROR_INVALID_ELEMENT 414
#define JANUS_TEXTROOM_ERROR_INVALID_REQUEST 415
#define JANUS_TEXTROOM_ERROR_ALREADY_SETUP 416
#define JANUS_TEXTROOM_ERROR_NO_SUCH_ROOM 417
#define JANUS_TEXTROOM_ERROR_ROOM_EXISTS 418
#define JANUS_TEXTROOM_ERROR_UNAUTHORIZED 419
#define JANUS_TEXTROOM_ERROR_USERNAME_EXISTS 420
#define JANUS_TEXTROOM_ERROR_ALREADY_IN_ROOM 421
#define JANUS_TEXTROOM_ERROR_NOT_IN_ROOM 422
#define JANUS_TEXTROOM_ERROR_UNKNOWN_ERROR 499
/* TextRoom watchdog/garbage collector (sort of) */
static void *janus_textroom_watchdog(void *data) {
JANUS_LOG(LOG_INFO, "TextRoom watchdog started\n");
gint64 now = 0;
while(g_atomic_int_get(&initialized) && !g_atomic_int_get(&stopping)) {
janus_mutex_lock(&sessions_mutex);
/* Iterate on all the sessions */
now = janus_get_monotonic_time();
if(old_sessions != NULL) {
GList *sl = old_sessions;
JANUS_LOG(LOG_HUGE, "Checking %d old TextRoom sessions...\n", g_list_length(old_sessions));
while(sl) {
janus_textroom_session *session = (janus_textroom_session *)sl->data;
if(!session) {
sl = sl->next;
continue;
}
if(now-session->destroyed >= 5*G_USEC_PER_SEC) {
/* We're lazy and actually get rid of the stuff only after a few seconds */
JANUS_LOG(LOG_VERB, "Freeing old TextRoom session\n");
GList *rm = sl->next;
old_sessions = g_list_delete_link(old_sessions, sl);
sl = rm;
session->handle = NULL;
/* TODO Free session stuff */
g_free(session);
session = NULL;
continue;
}
sl = sl->next;
}
}
janus_mutex_unlock(&sessions_mutex);
janus_mutex_lock(&rooms_mutex);
if(old_rooms != NULL) {
GList *rl = old_rooms;
now = janus_get_monotonic_time();
while(rl) {
janus_textroom_room *textroom = (janus_textroom_room*)rl->data;
if(!g_atomic_int_get(&initialized) || g_atomic_int_get(&stopping)){
break;
}
if(!textroom) {
rl = rl->next;
continue;
}
if(now - textroom->destroyed >= 5*G_USEC_PER_SEC) {
/* Free resources */
JANUS_LOG(LOG_VERB, "Freeing old TextRoom room %"SCNu64"\n", textroom->room_id);
g_free(textroom->room_name);
g_free(textroom->room_secret);
g_free(textroom->room_pin);
g_hash_table_destroy(textroom->participants);
g_free(textroom);
/* Move on */
GList *rm = rl->next;
old_rooms = g_list_delete_link(old_rooms, rl);
rl = rm;
continue;
}
rl = rl->next;
}
}
janus_mutex_unlock(&rooms_mutex);
g_usleep(500000);
}
JANUS_LOG(LOG_INFO, "TextRoom watchdog stopped\n");
return NULL;
}
#ifdef HAVE_LIBCURL
static size_t janus_textroom_write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
return size*nmemb;
}
#endif
void janus_textroom_handle_incoming_request(janus_plugin_session *handle, char *text, gboolean internal);
/* Plugin implementation */
int janus_textroom_init(janus_callbacks *callback, const char *config_path) {
if(g_atomic_int_get(&stopping)) {
/* Still stopping from before */
return -1;
}
if(callback == NULL || config_path == NULL) {
/* Invalid arguments */
return -1;
}
/* Read configuration */
char filename[255];
g_snprintf(filename, 255, "%s/%s.cfg", config_path, JANUS_TEXTROOM_PACKAGE);
JANUS_LOG(LOG_VERB, "Configuration file: %s\n", filename);
config = janus_config_parse(filename);
config_folder = config_path;
if(config != NULL)
janus_config_print(config);
janus_mutex_init(&config_mutex);
rooms = g_hash_table_new_full(g_int64_hash, g_int64_equal, (GDestroyNotify)g_free, NULL);
janus_mutex_init(&rooms_mutex);
sessions = g_hash_table_new(NULL, NULL);
messages = g_async_queue_new_full((GDestroyNotify) janus_textroom_message_free);
janus_mutex_init(&sessions_mutex);
/* This is the callback we'll need to invoke to contact the gateway */
gateway = callback;
/* Parse configuration to populate the rooms list */
if(config != NULL) {
janus_config_item *item = janus_config_get_item_drilldown(config, "general", "json");
if(item && item->value) {
/* Check how we need to format/serialize the JSON output */
if(!strcasecmp(item->value, "indented")) {
/* Default: indented, we use three spaces for that */
json_format = JSON_INDENT(3) | JSON_PRESERVE_ORDER;
} else if(!strcasecmp(item->value, "plain")) {
/* Not indented and no new lines, but still readable */
json_format = JSON_INDENT(0) | JSON_PRESERVE_ORDER;
} else if(!strcasecmp(item->value, "compact")) {
/* Compact, so no spaces between separators */
json_format = JSON_COMPACT | JSON_PRESERVE_ORDER;
} else {
JANUS_LOG(LOG_WARN, "Unsupported JSON format option '%s', using default (indented)\n", item->value);
json_format = JSON_INDENT(3) | JSON_PRESERVE_ORDER;
}
}
/* Any admin key to limit who can "create"? */
janus_config_item *key = janus_config_get_item_drilldown(config, "general", "admin_key");
if(key != NULL && key->value != NULL)
admin_key = g_strdup(key->value);
/* Iterate on all rooms */
GList *cl = janus_config_get_categories(config);
while(cl != NULL) {
janus_config_category *cat = (janus_config_category *)cl->data;
if(cat->name == NULL || !strcasecmp(cat->name, "general")) {
cl = cl->next;
continue;
}
JANUS_LOG(LOG_VERB, "Adding text room '%s'\n", cat->name);
janus_config_item *desc = janus_config_get_item(cat, "description");
janus_config_item *priv = janus_config_get_item(cat, "is_private");
janus_config_item *secret = janus_config_get_item(cat, "secret");
janus_config_item *pin = janus_config_get_item(cat, "pin");
janus_config_item *post = janus_config_get_item(cat, "post");
/* Create the text room */
janus_textroom_room *textroom = g_malloc0(sizeof(janus_textroom_room));
textroom->room_id = g_ascii_strtoull(cat->name, NULL, 0);
char *description = NULL;
if(desc != NULL && desc->value != NULL && strlen(desc->value) > 0)
description = g_strdup(desc->value);
else
description = g_strdup(cat->name);
textroom->room_name = description;
textroom->is_private = priv && priv->value && janus_is_true(priv->value);
if(secret != NULL && secret->value != NULL) {
textroom->room_secret = g_strdup(secret->value);
}
if(pin != NULL && pin->value != NULL) {
textroom->room_pin = g_strdup(pin->value);
}
if(post != NULL && post->value != NULL) {
#ifdef HAVE_LIBCURL
/* FIXME Should we check if this is a valid HTTP address? */
textroom->http_backend = g_strdup(post->value);
#else
JANUS_LOG(LOG_WARN, "HTTP backend specified, but libcurl support was not built in...\n");
#endif
}
textroom->participants = g_hash_table_new(g_str_hash, g_str_equal);
textroom->destroyed = 0;
janus_mutex_init(&textroom->mutex);
JANUS_LOG(LOG_VERB, "Created textroom: %"SCNu64" (%s, %s, secret: %s, pin: %s)\n",
textroom->room_id, textroom->room_name,
textroom->is_private ? "private" : "public",
textroom->room_secret ? textroom->room_secret : "no secret",
textroom->room_pin ? textroom->room_pin : "no pin");
g_hash_table_insert(rooms, janus_uint64_dup(textroom->room_id), textroom);
cl = cl->next;
}
/* Done: we keep the configuration file open in case we get a "create" or "destroy" with permanent=true */
}
/* Show available rooms */
janus_mutex_lock(&rooms_mutex);
GHashTableIter iter;
gpointer value;
g_hash_table_iter_init(&iter, rooms);
while (g_hash_table_iter_next(&iter, NULL, &value)) {
janus_textroom_room *tr = value;
JANUS_LOG(LOG_VERB, " ::: [%"SCNu64"][%s]\n", tr->room_id, tr->room_name);
}
janus_mutex_unlock(&rooms_mutex);
#ifdef HAVE_LIBCURL
curl_global_init(CURL_GLOBAL_ALL);
#endif
GError *error = NULL;
/* Start the sessions watchdog */
watchdog = g_thread_try_new("textroom watchdog", &janus_textroom_watchdog, NULL, &error);
if(error != NULL) {
g_atomic_int_set(&initialized, 0);
JANUS_LOG(LOG_ERR, "Got error %d (%s) trying to launch the TextRoom watchdog thread...\n", error->code, error->message ? error->message : "??");
return -1;
}
/* Launch the thread that will handle incoming messages */
handler_thread = g_thread_try_new("textroom handler", janus_textroom_handler, NULL, &error);
if(error != NULL) {
g_atomic_int_set(&initialized, 0);
JANUS_LOG(LOG_ERR, "Got error %d (%s) trying to launch the EchoTest handler thread...\n", error->code, error->message ? error->message : "??");
return -1;
}
g_atomic_int_set(&initialized, 1);
JANUS_LOG(LOG_INFO, "%s initialized!\n", JANUS_TEXTROOM_NAME);
return 0;
}
void janus_textroom_destroy(void) {
if(!g_atomic_int_get(&initialized))
return;
g_atomic_int_set(&stopping, 1);
g_async_queue_push(messages, &exit_message);
if(handler_thread != NULL) {
g_thread_join(handler_thread);
handler_thread = NULL;
}
if(watchdog != NULL) {
g_thread_join(watchdog);
watchdog = NULL;
}
/* FIXME We should destroy the sessions cleanly */
janus_mutex_lock(&sessions_mutex);
g_hash_table_destroy(sessions);
janus_mutex_unlock(&sessions_mutex);
janus_mutex_lock(&rooms_mutex);
g_hash_table_destroy(rooms);
janus_mutex_unlock(&rooms_mutex);
g_async_queue_unref(messages);
messages = NULL;
sessions = NULL;
#ifdef HAVE_LIBCURL
curl_global_cleanup();
#endif
janus_config_destroy(config);
g_free(admin_key);
g_atomic_int_set(&initialized, 0);
g_atomic_int_set(&stopping, 0);
JANUS_LOG(LOG_INFO, "%s destroyed!\n", JANUS_TEXTROOM_NAME);
}
int janus_textroom_get_api_compatibility(void) {
/* Important! This is what your plugin MUST always return: don't lie here or bad things will happen */
return JANUS_PLUGIN_API_VERSION;
}
int janus_textroom_get_version(void) {
return JANUS_TEXTROOM_VERSION;
}
const char *janus_textroom_get_version_string(void) {
return JANUS_TEXTROOM_VERSION_STRING;
}
const char *janus_textroom_get_description(void) {
return JANUS_TEXTROOM_DESCRIPTION;
}
const char *janus_textroom_get_name(void) {
return JANUS_TEXTROOM_NAME;
}
const char *janus_textroom_get_author(void) {
return JANUS_TEXTROOM_AUTHOR;
}
const char *janus_textroom_get_package(void) {
return JANUS_TEXTROOM_PACKAGE;
}
void janus_textroom_create_session(janus_plugin_session *handle, int *error) {
if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized)) {
*error = -1;
return;
}
janus_textroom_session *session = (janus_textroom_session *)g_malloc0(sizeof(janus_textroom_session));
session->handle = handle;
session->rooms = g_hash_table_new_full(g_int64_hash, g_int64_equal, (GDestroyNotify)g_free, NULL);
session->destroyed = 0;
janus_mutex_init(&session->mutex);
g_atomic_int_set(&session->setup, 0);
g_atomic_int_set(&session->hangingup, 0);
handle->plugin_handle = session;
janus_mutex_lock(&sessions_mutex);
g_hash_table_insert(sessions, handle, session);
janus_mutex_unlock(&sessions_mutex);
return;
}
void janus_textroom_destroy_session(janus_plugin_session *handle, int *error) {
if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized)) {
*error = -1;
return;
}
janus_textroom_session *session = (janus_textroom_session *)handle->plugin_handle;
if(!session) {
JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
*error = -2;
return;
}
JANUS_LOG(LOG_VERB, "Removing Echo Test session...\n");
janus_mutex_lock(&sessions_mutex);
if(!session->destroyed) {
g_hash_table_remove(sessions, handle);
janus_textroom_hangup_media(handle);
session->destroyed = janus_get_monotonic_time();
/* Cleaning up and removing the session is done in a lazy way */
old_sessions = g_list_append(old_sessions, session);
}
janus_mutex_unlock(&sessions_mutex);
return;
}
json_t *janus_textroom_query_session(janus_plugin_session *handle) {
if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized)) {
return NULL;
}
janus_textroom_session *session = (janus_textroom_session *)handle->plugin_handle;
if(!session) {
JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
return NULL;
}
/* TODO Return meaningful info: participant details, rooms they're in, etc. */
json_t *info = json_object();
json_object_set_new(info, "destroyed", json_integer(session->destroyed));
return info;
}
struct janus_plugin_result *janus_textroom_handle_message(janus_plugin_session *handle, char *transaction, json_t *message, json_t *jsep) {
if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
return janus_plugin_result_new(JANUS_PLUGIN_ERROR, g_atomic_int_get(&stopping) ? "Shutting down" : "Plugin not initialized", NULL);
janus_textroom_message *msg = g_malloc0(sizeof(janus_textroom_message));
msg->handle = handle;
msg->transaction = transaction;
msg->message = message;
msg->jsep = jsep;
g_async_queue_push(messages, msg);
/* All the requests to this plugin are handled asynchronously */
return janus_plugin_result_new(JANUS_PLUGIN_OK_WAIT, "I'm taking my time!", NULL);
}
void janus_textroom_setup_media(janus_plugin_session *handle) {
JANUS_LOG(LOG_INFO, "WebRTC media is now available\n");
if(g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
return;
janus_textroom_session *session = (janus_textroom_session *)handle->plugin_handle;
if(!session) {
JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
return;
}
if(session->destroyed)
return;
g_atomic_int_set(&session->hangingup, 0);
}
void janus_textroom_incoming_rtp(janus_plugin_session *handle, int video, char *buf, int len) {
/* We don't do audio/video */
}
void janus_textroom_incoming_rtcp(janus_plugin_session *handle, int video, char *buf, int len) {
/* We don't do audio/video */
}
void janus_textroom_incoming_data(janus_plugin_session *handle, char *buf, int len) {
if(handle == NULL || handle->stopped || g_atomic_int_get(&stopping) || !g_atomic_int_get(&initialized))
return;
/* Incoming request from this user: what should we do? */
janus_textroom_session *session = (janus_textroom_session *)handle->plugin_handle;
if(!session) {
JANUS_LOG(LOG_ERR, "No session associated with this handle...\n");
return;
}
if(session->destroyed)
return;
if(buf == NULL || len <= 0)
return;
char *text = g_malloc0(len+1);
memcpy(text, buf, len);
*(text+len) = '\0';
JANUS_LOG(LOG_VERB, "Got a DataChannel message (%zu bytes): %s\n", strlen(text), text);
janus_textroom_handle_incoming_request(handle, text, FALSE);
}
/* Helper method to handle incoming messages from the data channel */
void janus_textroom_handle_incoming_request(janus_plugin_session *handle, char *text, gboolean internal) {
janus_textroom_session *session = (janus_textroom_session *)handle->plugin_handle;
/* Parse JSON */
json_error_t error;
json_t *root = json_loads(text, 0, &error);
g_free(text);
if(!root) {
JANUS_LOG(LOG_ERR, "Error parsing data channel message (JSON error: on line %d: %s)\n", error.line, error.text);
return;
}
/* Handle request */
int error_code = 0;
char error_cause[512];
JANUS_VALIDATE_JSON_OBJECT(root, transaction_parameters,
error_code, error_cause, TRUE,
JANUS_TEXTROOM_ERROR_MISSING_ELEMENT, JANUS_TEXTROOM_ERROR_INVALID_ELEMENT);
const char *transaction_text = NULL;
if(error_code != 0)
goto error;
json_t *request = json_object_get(root, "textroom");
json_t *transaction = json_object_get(root, "transaction");
const char *request_text = json_string_value(request);
transaction_text = json_string_value(transaction);
if(!strcasecmp(request_text, "message")) {
JANUS_VALIDATE_JSON_OBJECT(root, message_parameters,
error_code, error_cause, TRUE,
JANUS_TEXTROOM_ERROR_MISSING_ELEMENT, JANUS_TEXTROOM_ERROR_INVALID_ELEMENT);
if(error_code != 0)
goto error;
json_t *room = json_object_get(root, "room");
guint64 room_id = json_integer_value(room);
janus_mutex_lock(&rooms_mutex);
janus_textroom_room *textroom = g_hash_table_lookup(rooms, &room_id);
if(textroom == NULL) {
janus_mutex_unlock(&rooms_mutex);
JANUS_LOG(LOG_ERR, "No such room (%"SCNu64")\n", room_id);
error_code = JANUS_TEXTROOM_ERROR_NO_SUCH_ROOM;
g_snprintf(error_cause, 512, "No such room (%"SCNu64")", room_id);
goto error;
}
janus_mutex_lock(&textroom->mutex);
janus_mutex_unlock(&rooms_mutex);
janus_textroom_participant *participant = g_hash_table_lookup(session->rooms, &room_id);
if(participant == NULL) {
janus_mutex_unlock(&textroom->mutex);
JANUS_LOG(LOG_ERR, "Not in room %"SCNu64"\n", room_id);
error_code = JANUS_TEXTROOM_ERROR_NOT_IN_ROOM;
g_snprintf(error_cause, 512, "Not in room %"SCNu64, room_id);
goto error;
}
json_t *username = json_object_get(root, "to");
json_t *usernames = json_object_get(root, "tos");
if(username && usernames) {
janus_mutex_unlock(&textroom->mutex);
JANUS_LOG(LOG_ERR, "Both to and tos array provided\n");
error_code = JANUS_TEXTROOM_ERROR_INVALID_ELEMENT;
g_snprintf(error_cause, 512, "Both to and tos array provided");
goto error;
}
json_t *text = json_object_get(root, "text");
const char *message = json_string_value(text);
/* Prepare outgoing message */
json_t *msg = json_object();
json_object_set_new(msg, "textroom", json_string("message"));
json_object_set_new(msg, "room", json_integer(room_id));
json_object_set_new(msg, "from", json_string(participant->username));
time_t timer;
time(&timer);
struct tm *tm_info = localtime(&timer);
char msgTime[64];
strftime(msgTime, sizeof(msgTime), "%FT%T%z", tm_info);
json_object_set_new(msg, "date", json_string(msgTime));
json_object_set_new(msg, "text", json_string(message));
if(username || usernames)
json_object_set_new(msg, "whisper", json_true());
char *msg_text = json_dumps(msg, json_format);
json_decref(msg);
/* Start preparing the response too */
json_t *reply = json_object();
json_object_set_new(reply, "textroom", json_string("success"));
json_object_set_new(reply, "transaction", json_string(transaction_text));
/* Who should we send this message to? */
if(username) {
/* A single user */
json_t *sent = json_object();
const char *to = json_string_value(username);
JANUS_LOG(LOG_VERB, "To %s in %"SCNu64": %s\n", to, room_id, message);
janus_textroom_participant *top = g_hash_table_lookup(textroom->participants, to);
if(top) {
gateway->relay_data(top->session->handle, msg_text, strlen(msg_text));
json_object_set_new(sent, to, json_true());
} else {
JANUS_LOG(LOG_WARN, "User %s is not in room %"SCNu64", failed to send message\n", to, room_id);
json_object_set_new(sent, to, json_false());
}
json_object_set_new(reply, "sent", sent);
} else if(usernames) {
/* A limited number of users */
json_t *sent = json_object();
size_t i = 0;
for(i=0; i<json_array_size(usernames); i++) {
json_t *u = json_array_get(usernames, i);
const char *to = json_string_value(u);
JANUS_LOG(LOG_VERB, "To %s in %"SCNu64": %s\n", to, room_id, message);
janus_textroom_participant *top = g_hash_table_lookup(textroom->participants, to);
if(top) {
gateway->relay_data(top->session->handle, msg_text, strlen(msg_text));
json_object_set_new(sent, to, json_true());
} else {
JANUS_LOG(LOG_WARN, "User %s is not in room %"SCNu64", failed to send message\n", to, room_id);
json_object_set_new(sent, to, json_false());
}
}
json_object_set_new(reply, "sent", sent);
} else {
/* Everybody in the room */
JANUS_LOG(LOG_VERB, "To everybody in %"SCNu64": %s\n", room_id, message);
if(textroom->participants) {
GHashTableIter iter;
gpointer value;
g_hash_table_iter_init(&iter, textroom->participants);
while(g_hash_table_iter_next(&iter, NULL, &value)) {
janus_textroom_participant *top = value;
JANUS_LOG(LOG_VERB, " >> To %s in %"SCNu64": %s\n", top->username, room_id, message);
gateway->relay_data(top->session->handle, msg_text, strlen(msg_text));
}
}
#ifdef HAVE_LIBCURL
/* Is there a backend waiting for this message too? */
if(textroom->http_backend) {
/* Prepare the libcurl context */
CURLcode res;
CURL *curl = curl_easy_init();
if(curl == NULL) {
JANUS_LOG(LOG_ERR, "Error initializing CURL context\n");
} else {
curl_easy_setopt(curl, CURLOPT_URL, textroom->http_backend);
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charsets: utf-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, msg_text);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, janus_textroom_write_data);
/* Send the request */
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
JANUS_LOG(LOG_ERR, "Couldn't relay event to the backend: %s\n", curl_easy_strerror(res));
} else {
JANUS_LOG(LOG_DBG, "Event sent!\n");
}
}
}
#endif
}
g_free(msg_text);
janus_mutex_unlock(&textroom->mutex);
/* By default we send a confirmation back to the user that sent this message:
* if the user passed an ack=false, though, we don't do that */
json_t *ack = json_object_get(root, "ack");
if(!internal && (ack == NULL || json_is_true(ack))) {
/* Send response back */
char *reply_text = json_dumps(reply, json_format);
gateway->relay_data(handle, reply_text, strlen(reply_text));
g_free(reply_text);
}
json_decref(reply);
} else if(!strcasecmp(request_text, "join")) {
JANUS_VALIDATE_JSON_OBJECT(root, join_parameters,
error_code, error_cause, TRUE,
JANUS_TEXTROOM_ERROR_MISSING_ELEMENT, JANUS_TEXTROOM_ERROR_INVALID_ELEMENT);
if(error_code != 0)
goto error;
json_t *room = json_object_get(root, "room");
guint64 room_id = json_integer_value(room);
janus_mutex_lock(&rooms_mutex);
janus_textroom_room *textroom = g_hash_table_lookup(rooms, &room_id);
if(textroom == NULL) {
janus_mutex_unlock(&rooms_mutex);
JANUS_LOG(LOG_ERR, "No such room (%"SCNu64")\n", room_id);
error_code = JANUS_TEXTROOM_ERROR_NO_SUCH_ROOM;
g_snprintf(error_cause, 512, "No such room (%"SCNu64")", room_id);
goto error;
}
janus_mutex_lock(&textroom->mutex);
janus_mutex_unlock(&rooms_mutex);
janus_mutex_lock(&session->mutex);
if(g_hash_table_lookup(session->rooms, &room_id) != NULL) {
janus_mutex_unlock(&session->mutex);
janus_mutex_unlock(&textroom->mutex);
JANUS_LOG(LOG_ERR, "Already in room %"SCNu64"\n", room_id);
error_code = JANUS_TEXTROOM_ERROR_ALREADY_IN_ROOM;
g_snprintf(error_cause, 512, "Already in room %"SCNu64, room_id);
goto error;
}
json_t *username = json_object_get(root, "username");
const char *username_text = json_string_value(username);
janus_textroom_participant *participant = g_hash_table_lookup(textroom->participants, username_text);
if(participant != NULL) {
janus_mutex_unlock(&session->mutex);
janus_mutex_unlock(&textroom->mutex);
JANUS_LOG(LOG_ERR, "Username already taken\n");
error_code = JANUS_TEXTROOM_ERROR_USERNAME_EXISTS;
g_snprintf(error_cause, 512, "Username already taken");
goto error;
}
json_t *display = json_object_get(root, "display");
const char *display_text = json_string_value(display);
/* Create a participant instance */
participant = g_malloc0(sizeof(janus_textroom_participant));
participant->session = session;
participant->room = textroom;
participant->username = g_strdup(username_text);
participant->display = display_text ? g_strdup(display_text) : NULL;
participant->destroyed = 0;
janus_mutex_init(&participant->mutex);
g_hash_table_insert(session->rooms, janus_uint64_dup(textroom->room_id), participant);
g_hash_table_insert(textroom->participants, participant->username, participant);
/* Notify all participants */
JANUS_LOG(LOG_VERB, "Notifying all participants about the new join\n");
json_t *list = json_array();
if(textroom->participants) {
/* Prepare event */
json_t *event = json_object();
json_object_set_new(event, "textroom", json_string("join"));
json_object_set_new(event, "room", json_integer(textroom->room_id));
json_object_set_new(event, "username", json_string(username_text));
if(display_text != NULL)
json_object_set_new(event, "display", json_string(display_text));
char *event_text = json_dumps(event, json_format);
json_decref(event);
gateway->relay_data(handle, event_text, strlen(event_text));
/* Broadcast */
GHashTableIter iter;
gpointer value;
g_hash_table_iter_init(&iter, textroom->participants);
while(g_hash_table_iter_next(&iter, NULL, &value)) {
janus_textroom_participant *top = value;
if(top == participant)
continue; /* Skip us */
JANUS_LOG(LOG_VERB, " >> To %s in %"SCNu64"\n", top->username, room_id);
gateway->relay_data(top->session->handle, event_text, strlen(event_text));
/* Take note of this user */
json_t *p = json_object();
json_object_set_new(p, "username", json_string(top->username));
if(top->display != NULL)
json_object_set_new(p, "display", json_string(top->display));
json_array_append_new(list, p);
}
g_free(event_text);
}
janus_mutex_unlock(&session->mutex);
janus_mutex_unlock(&textroom->mutex);
if(!internal) {
/* Send response back */
json_t *reply = json_object();
json_object_set_new(reply, "textroom", json_string("success"));
json_object_set_new(reply, "transaction", json_string(transaction_text));
json_object_set_new(reply, "participants", list);
char *reply_text = json_dumps(reply, json_format);
json_decref(reply);
gateway->relay_data(handle, reply_text, strlen(reply_text));
g_free(reply_text);
}
} else if(!strcasecmp(request_text, "leave")) {
JANUS_VALIDATE_JSON_OBJECT(root, room_parameters,
error_code, error_cause, TRUE,
JANUS_TEXTROOM_ERROR_MISSING_ELEMENT, JANUS_TEXTROOM_ERROR_INVALID_ELEMENT);
if(error_code != 0)
goto error;
json_t *room = json_object_get(root, "room");
guint64 room_id = json_integer_value(room);
janus_mutex_lock(&rooms_mutex);
janus_textroom_room *textroom = g_hash_table_lookup(rooms, &room_id);
if(textroom == NULL) {
janus_mutex_unlock(&rooms_mutex);
JANUS_LOG(LOG_ERR, "No such room (%"SCNu64")\n", room_id);
error_code = JANUS_TEXTROOM_ERROR_NO_SUCH_ROOM;
g_snprintf(error_cause, 512, "No such room (%"SCNu64")", room_id);
goto error;
}
janus_mutex_lock(&textroom->mutex);
janus_mutex_unlock(&rooms_mutex);
janus_mutex_lock(&session->mutex);
janus_textroom_participant *participant = g_hash_table_lookup(session->rooms, &room_id);
if(participant == NULL) {
janus_mutex_unlock(&session->mutex);
janus_mutex_unlock(&textroom->mutex);
JANUS_LOG(LOG_ERR, "Not in room %"SCNu64"\n", room_id);
error_code = JANUS_TEXTROOM_ERROR_NOT_IN_ROOM;
g_snprintf(error_cause, 512, "Not in room %"SCNu64, room_id);
goto error;
}
g_hash_table_remove(session->rooms, &room_id);
g_hash_table_remove(textroom->participants, participant->username);
participant->session = NULL;
participant->room = NULL;
/* Notify all participants */
JANUS_LOG(LOG_VERB, "Notifying all participants about the new leave\n");
if(textroom->participants) {
/* Prepare event */
json_t *event = json_object();
json_object_set_new(event, "textroom", json_string("leave"));
json_object_set_new(event, "room", json_integer(textroom->room_id));
json_object_set_new(event, "username", json_string(participant->username));
char *event_text = json_dumps(event, json_format);
json_decref(event);
gateway->relay_data(handle, event_text, strlen(event_text));
/* Broadcast */
GHashTableIter iter;
gpointer value;
g_hash_table_iter_init(&iter, textroom->participants);
while(g_hash_table_iter_next(&iter, NULL, &value)) {
janus_textroom_participant *top = value;
if(top == participant)
continue; /* Skip us */
JANUS_LOG(LOG_VERB, " >> To %s in %"SCNu64"\n", top->username, room_id);
gateway->relay_data(top->session->handle, event_text, strlen(event_text));
}
g_free(event_text);
}
g_free(participant->username);
g_free(participant->display);
g_free(participant);
janus_mutex_unlock(&session->mutex);
janus_mutex_unlock(&textroom->mutex);
if(!internal) {
/* Send response back */
json_t *reply = json_object();
json_object_set_new(reply, "textroom", json_string("success"));
json_object_set_new(reply, "transaction", json_string(transaction_text));
char *reply_text = json_dumps(reply, json_format);