-
Notifications
You must be signed in to change notification settings - Fork 5
/
psclient.c
1891 lines (1764 loc) · 80.8 KB
/
psclient.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
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <dirent.h>
#include <net/if.h>
#include <netdb.h>
#include <sys/stat.h>
#include <poll.h>
#include <sys/resource.h>
#include "psclient.h"
#include "apierror.h"
#include "rtcp.h"
#include "sdp.h"
#include "debug.h"
#include "log.h"
#include "config.h"
#include "utils.h"
int lock_debug = 0;
int ps_log_level = LOG_DBG;
gboolean ps_log_timestamps = TRUE;
gboolean ps_log_colors = TRUE;
static ps_config * config = NULL;
static char * config_file = NULL;
static char * configs_folder = NULL;
static GHashTable * plugins = NULL;
static char * server_key = NULL;
gchar * ps_get_server_key (void) {
return server_key;
}
static char * server_pem = NULL;
gchar * ps_get_server_pem (void) {
return server_pem;
}
static gchar local_ip[INET6_ADDRSTRLEN];
gchar * ps_get_local_ip (void) {
return local_ip;
}
static gchar * public_ip = NULL;
gchar * ps_get_public_ip (void) {
return public_ip ? public_ip : local_ip;
}
void ps_set_public_ip (const char * ip) {
if (ip == NULL || public_ip != NULL) return;
public_ip = g_strdup(ip);
}
static volatile gint stop = 0;
gint ps_is_stopping(void) {
return g_atomic_int_get(&stop);
}
/* Transport plugin callback interface */
void ps_transport_incoming_request (ps_transport * plugin, void * transport, json_t * message, json_error_t * error);
void ps_transport_gone (ps_transport * plugin, void * transport);
static ps_transport_callbacks ps_handler = {
.incoming_request = ps_transport_incoming_request,
.transport_gone = ps_transport_gone,
};
GThreadPool * tasks = NULL;
void ps_transport_task (gpointer data, gpointer user_data);
/* plugin callback interface */
int ps_plugin_push_event (ps_plugin_session * plugin_session, ps_plugin * plugin, const char * transaction, const char * message, const char * sdp_type, const char * sdp);
json_t * ps_plugin_handle_sdp (ps_plugin_session * plugin_session, ps_plugin * plugin, const char * sdp_type, const char * sdp);
void ps_plugin_relay_rtp (ps_plugin_session * plugin_session, int video, char * buf, int len);
void ps_plugin_relay_rtcp (ps_plugin_session * plugin_session, int video, char * buf, int len);
void ps_plugin_relay_data (ps_plugin_session * plugin_session, char * buf, int len);
void ps_plugin_close_pc (ps_plugin_session * plugin_session);
void ps_plugin_end_session (ps_plugin_session * plugin_session);
static ps_callbacks ps_handler_plugin = {
.push_event = ps_plugin_push_event,
.relay_rtp = ps_plugin_relay_rtp,
.relay_rtcp = ps_plugin_relay_rtcp,
.relay_data = ps_plugin_relay_data,
.close_pc = ps_plugin_close_pc,
.end_session = ps_plugin_end_session,
};
/* Gateway sessions */
static GMainContext * sessions_watchdog_context;
static ps_mutex sessions_mutex;
static GHashTable * sessions = NULL, * old_sessions = NULL;
#define SESSION_TIMEOUT 60
static gboolean ps_cleanup_session (gpointer user_data) {
ps_session * session = (ps_session *) user_data;
PS_LOG (LOG_DBG, "Cleaning up session %"SCNu64"...\n", session->session_id);
ps_session_destroy (session->session_id);
return G_SOURCE_REMOVE;
}
static gboolean ps_check_sessions (gpointer user_data) {
GMainContext * watchdog_context = (GMainContext *) user_data;
ps_mutex_lock (&sessions_mutex);
if (sessions && g_hash_table_size (sessions) > 0) {
GHashTableIter iter;
gpointer value;
g_hash_table_iter_init (&iter, sessions);
while (g_hash_table_iter_next(&iter, NULL, &value)) {
ps_session * session = (ps_session *) value;
if (!session || session->destroy) {
continue;
}
gint64 now = ps_get_monotonic_time ();
if ((now - session->last_activity) >= SESSION_TIMEOUT * G_USEC_PER_SEC && !session->timeout) {
PS_LOG (LOG_INFO, "Timeout expired for session %"SCNu64"...\n", session->session_id);
if (session->source) {
json_t * event = json_object ();
json_object_set_new (event, "janus", json_string("timeout"));
json_object_set_new (event, "session_id", json_integer(session->session_id));
session->source->transport->send_message (session->source->instance, event);
session->source->transport->session_over (session->source->instance, session->session_id, TRUE);
}
session->timeout = 1;
g_hash_table_iter_remove (&iter);
g_hash_table_insert (old_sessions, GUINT_TO_POINTER (session->session_id), session);
/* Schedule session for deletion */
GSource * timeout_source = g_timeout_source_new_seconds (3);
g_source_set_callback (timeout_source, ps_cleanup_session, session, NULL);
g_source_attach (timeout_source, watchdog_context);
g_source_unref (timeout_source);
}
}
}
ps_mutex_unlock (&sessions_mutex);
return G_SOURCE_CONTINUE;
}
static gpointer ps_sessions_watchdog (gpointer user_data) {
GMainLoop * loop = (GMainLoop *) user_data;
GMainContext * watchdog_context = g_main_loop_get_context (loop);
GSource * timeout_source;
timeout_source = g_timeout_source_new_seconds (2);
g_source_set_callback (timeout_source, ps_check_sessions, watchdog_context, NULL);
g_source_attach (timeout_source, watchdog_context);
g_source_unref (timeout_source);
PS_LOG (LOG_INFO, "Session watchdog started...\n");
g_main_loop_run (loop);
return NULL;
}
ps_session * ps_session_create (guint64 session_id) {
if (session_id == 0) {
while (session_id == 0) {
session_id = g_random_int ();
if (ps_session_find (session_id) != NULL) {
session_id = 0;
}
}
}
PS_LOG (LOG_INFO, "Creating new session: %"SCNu64"\n",session_id);
ps_session * session = (ps_session *)g_malloc0(sizeof(ps_session));
if (session == NULL) {
PS_LOG (LOG_FATAL, "Memory error!\n");
return NULL;
}
session->session_id = session_id;
session->source = NULL;
session->destroy = 0;
session->timeout = 0;
session->last_activity = ps_get_monotonic_time();
ps_mutex_init (&session->mutex);
ps_mutex_lock (&session->mutex);
g_hash_table_insert (sessions, GUINT_TO_POINTER (session_id), session);
ps_mutex_unlock (&session->mutex);
return session;
}
ps_session * ps_session_find (guint64 session_id) {
ps_mutex_lock (&sessions_mutex);
ps_session * session = g_hash_table_lookup (sessions, GUINT_TO_POINTER(session_id));
ps_mutex_unlock (&sessions_mutex);
return session;
}
ps_session * ps_session_find_destroyed (guint64 session_id) {
ps_mutex_lock (&sessions_mutex);
ps_session * session = g_hash_table_lookup (old_sessions, GUINT_TO_POINTER(session_id));
ps_mutex_unlock (&sessions_mutex);
return session;
}
void ps_session_notify_event (guint64 session_id, json_t * event) {
ps_mutex_lock (&sessions_mutex);
ps_session * session = sessions ? g_hash_table_lookup (sessions, GUINT_TO_POINTER(session_id)) : NULL;
if (session != NULL && !session->destroy && session->source != NULL && session->source->transport != NULL) {
ps_mutex_unlock (&sessions_mutex);
PS_LOG (LOG_HUGE, "Sending event to %p\n", session->source->instance);
session->source->transport->send_message(session->source->instance, event);
} else {
ps_mutex_unlock (&sessions_mutex);
json_decref (event);
}
}
gint ps_session_destroy (guint64 session_id) {
ps_session * session = ps_session_find_destroyed(session_id);
if (session == NULL) {
PS_LOG (LOG_ERR, "Couldn't find session to destroy: %"SCNu64"\n", session_id);
return -1;
}
PS_LOG (LOG_VERB, "Destroying session: %"SCNu64"\n", session_id);
session->destroy = 1;
if (session->ice_handles != NULL && g_hash_table_size(session->ice_handles) > 0) {
GHashTableIter iter;
gpointer value;
/* Remove all handles */
g_hash_table_iter_init(&iter, session->ice_handles);
while (g_hash_table_iter_next(&iter, NULL, &value)) {
janus_ice_handle *handle = value;
if(!handle || g_atomic_int_get(&stop)) {
continue;
}
janus_ice_handle_destroy(session, handle->handle_id);
g_hash_table_iter_remove(&iter);
}
}
ps_session_free (session);
return 0;
}
void ps_session_free (ps_session * session) {
if (session == NULL) return;
ps_mutex_lock (&session->mutex);
if (session->ice_handles != NULL) {
g_hash_table_destroy (session->ice_handles);
session->ice_handles = NULL;
}
if (session->source != NULL) {
ps_request_destroy (session->source);
session->source = NULL;
}
ps_mutex_unlock (&session->mutex);
g_free (session);
session = NULL;
}
/* Requests management */
ps_request * ps_request_new (ps_transport * transport, void * instance, json_t * message) {
ps_request * request = (ps_request *)g_malloc0(sizeof(ps_request));
request->transport = transport;
request->instance = instance;
request->message = message;
return request;
}
void ps_request_destroy (ps_request * request) {
if (request == NULL) return;
request->transport = NULL;
request->instance = NULL;
if (request->message) json_decref (request->message);
request->message = NULL;
g_free (request);
}
int ps_process_incoming_request (ps_request * request) {
int ret = -1;
if (request == NULL) {
PS_LOG (LOG_ERR, "missing request or payload to process, giving up...\n");
return ret;
}
json_t * root = request->message;
guint64 session_id = 0, handle_id = 0;
json_t * s = json_object_get(root, "session_id");
if (s && json_is_integer(s)) session_id = json_integer_value(s);
json_t * h = json_object_get(root, "handle_id");
if (h && json_is_integer(h)) handle_id = json_integer_value(h);
json_t * transaction = json_object_get (root, "transaction");
if (!transaction) {
ret = ps_process_error (request, session_id, NULL, JANUS_ERROR_MISSING_MANDATORY_ELEMENT, "Missing mandatory element (transaction)");
goto jsondone;
}
if (!json_is_string(transaction)) {
ret = ps_process_error (request, session_id, NULL, JANUS_ERROR_INVALID_ELEMENT_TYPE, "Invalid element type (transaction should be a string)");
goto jsondone;
}
const gchar * transaction_text = json_string_value(transaction);
json_t * message = json_object_get (root, "janus");
if (!message) {
ret = ps_process_error (request, session_id, transaction_text, JANUS_ERROR_MISSING_MANDATORY_ELEMENT, "Missing mandatory element (janus)");
goto jsondone;
}
if (!json_is_string(message)) {
ret = ps_process_error (request, session_id, transaction_text, JANUS_ERROR_INVALID_ELEMENT_TYPE, "Invalid element type (janus should be a string)");
goto jsondone;
}
const gchar * message_text = json_string_value(message);
if (session_id == 0 && handle_id == 0) {
if (strcasecmp(message_text, "create")) {
ret = ps_process_error (request, session_id, transaction_text, JANUS_ERROR_INVALID_REQUEST_PATH, "Unhandled request '%s' at this path", message_text);
goto jsondone;
}
session_id = 0;
json_t * id = json_object_get(root, "id");
if (id != NULL) {
if (!json_is_integer(id) || json_integer_value (id) < 0) {
ret = ps_process_error (request,session_id,transaction_text, JANUS_ERROR_INVALID_ELEMENT_TYPE, "Invalid element type, (id should be a positive integer");
goto jsondone;
}
session_id = json_integer_value (id);
if (session_id > 0 && ps_session_find(session_id) != NULL) {
ret = ps_process_error (request, session_id, transaction_text, JANUS_ERROR_SESSION_CONFLICT, "Session id already in use");
goto jsondone;
}
}
ps_session * session = ps_session_create(session_id);
if (session == NULL) {
ret = ps_process_error (request, session_id, transaction_text, JANUS_ERROR_UNKNOWN, "Memory Error");
goto jsondone;
}
session_id = session->session_id;
session->source = ps_request_new (request->transport, request->instance, NULL);
json_t * reply = json_object ();
json_object_set_new (reply, "janus", json_string("success"));
json_object_set_new (reply, "transaction", json_string(transaction_text));
json_t * data = json_object ();
json_object_set_new (data, "id", json_integer (session_id));
json_object_set_new (reply, "data", data);
ret = ps_process_success (request, reply);
goto jsondone;
}
if (session_id < 1) {
PS_LOG (LOG_ERR, "Invalid session\n");
ret = ps_process_error (request, session_id, transaction_text, JANUS_ERROR_SESSION_NOT_FOUND, NULL);
goto jsondone;
}
if (h && handle_id < 1) {
PS_LOG (LOG_ERR, "Invalid Handle\n");
ret = ps_process_error (request, session_id, transaction_text, JANUS_ERROR_SESSION_NOT_FOUND, NULL);
goto jsondone;
}
ps_session * session = ps_session_find (session_id);
if (!session) {
PS_LOG (LOG_ERR, "Couldn't find session %"SCNu64"...\n", session_id);
ret = ps_process_error (request, session_id, transaction_text, JANUS_ERROR_SESSION_NOT_FOUND, "No such session %"SCNu64"", session_id);
goto jsondone;
}
session->last_activity = ps_get_monotonic_time();
janus_ice_handle * handle = NULL;
if (handle_id > 0) {
handle = janus_ice_handle_find (session, handle_id);
if (!handle) {
PS_LOG (LOG_ERR, "Couldnt find any handle %"SCNu64" in session %"SCNu64"...\n", handle_id, session_id);
ret = ps_process_error (request, session_id, transaction_text, JANUS_ERROR_HANDLE_NOT_FOUND, "No such handle %"SCNu64" in session %"SCNu64"", handle_id, session_id);
goto jsondone;
}
}
if(!strcasecmp(message_text, "keepalive")) {
/* Just a keep-alive message, reply with an ack */
PS_LOG (LOG_VERB, "Got a keep-alive on session %"SCNu64"\n", session_id);
json_t *reply = json_object();
json_object_set_new (reply, "janus", json_string("ack"));
json_object_set_new (reply, "session_id", json_integer(session_id));
json_object_set_new (reply, "transaction", json_string(transaction_text));
/* Send the success reply */
ret = ps_process_success (request, reply);
} else if (!strcasecmp(message_text, "attach")) {
if(handle != NULL) {
/* Attach is a session-level command */
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_INVALID_REQUEST_PATH, "Unhandled request '%s' at this path", message_text);
goto jsondone;
}
json_t *plugin = json_object_get(root, "plugin");
if(!plugin) {
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_MISSING_MANDATORY_ELEMENT, "Missing mandatory element (plugin)");
goto jsondone;
}
if(!json_is_string(plugin)) {
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_INVALID_ELEMENT_TYPE, "Invalid element type (plugin should be a string)");
goto jsondone;
}
const gchar *plugin_text = json_string_value(plugin);
ps_plugin *plugin_t = ps_plugin_find(plugin_text);
if(plugin_t == NULL) {
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_PLUGIN_NOT_FOUND, "No such plugin '%s'", plugin_text);
goto jsondone;
}
/* Create handle */
handle = janus_ice_handle_create(session);
if(handle == NULL) {
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_UNKNOWN, "Memory error");
goto jsondone;
}
handle_id = handle->handle_id;
/* Attach to the plugin */
int error = 0;
if((error = janus_ice_handle_attach_plugin(session, handle_id, plugin_t)) != 0) {
/* TODO Make error struct to pass verbose information */
janus_ice_handle_destroy(session, handle_id);
ps_mutex_lock(&session->mutex);
g_hash_table_remove(session->ice_handles, GUINT_TO_POINTER(handle_id));
ps_mutex_unlock(&session->mutex);
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_PLUGIN_ATTACH, "Couldn't attach to plugin: error '%d'", error);
goto jsondone;
}
/* Prepare JSON reply */
json_t *reply = json_object();
json_object_set_new(reply, "janus", json_string("success"));
json_object_set_new(reply, "session_id", json_integer(session_id));
json_object_set_new(reply, "transaction", json_string(transaction_text));
json_t *data = json_object();
json_object_set_new(data, "id", json_integer(handle_id));
json_object_set_new(reply, "data", data);
/* Send the success reply */
ret = ps_process_success(request, reply);
} else if (!strcasecmp(message_text, "destroy")) {
if(handle != NULL) {
/* Query is a session-level command */
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_INVALID_REQUEST_PATH, "Unhandled request '%s' at this path", message_text);
goto jsondone;
}
/* Schedule the session for deletion */
session->destroy = 1;
ps_mutex_lock(&sessions_mutex);
g_hash_table_remove(sessions, GUINT_TO_POINTER(session->session_id));
g_hash_table_insert(old_sessions, GUINT_TO_POINTER(session->session_id), session);
GSource *timeout_source = g_timeout_source_new_seconds(3);
g_source_set_callback(timeout_source, ps_cleanup_session, session, NULL);
g_source_attach(timeout_source, sessions_watchdog_context);
g_source_unref(timeout_source);
ps_mutex_unlock(&sessions_mutex);
/* Notify the source that the session has been destroyed */
if(session->source && session->source->transport)
session->source->transport->session_over(session->source->instance, session->session_id, FALSE);
/* Prepare JSON reply */
json_t *reply = json_object();
json_object_set_new(reply, "janus", json_string("success"));
json_object_set_new(reply, "session_id", json_integer(session_id));
json_object_set_new(reply, "transaction", json_string(transaction_text));
/* Send the success reply */
ret = ps_process_success(request, reply);
} else if (!strcasecmp(message_text, "detach")) {
if(handle == NULL) {
/* Query is an handle-level command */
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_INVALID_REQUEST_PATH, "Unhandled request '%s' at this path", message_text);
goto jsondone;
}
if(handle->app == NULL || handle->app_handle == NULL) {
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_PLUGIN_DETACH, "No plugin to detach from");
goto jsondone;
}
int error = janus_ice_handle_destroy(session, handle_id);
ps_mutex_lock(&session->mutex);
g_hash_table_remove(session->ice_handles, GUINT_TO_POINTER(handle_id));
ps_mutex_unlock(&session->mutex);
if(error != 0) {
/* TODO Make error struct to pass verbose information */
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_PLUGIN_DETACH, "Couldn't detach from plugin: error '%d'", error);
/* TODO Delete handle instance */
goto jsondone;
}
/* Prepare JSON reply */
json_t *reply = json_object();
json_object_set_new(reply, "janus", json_string("success"));
json_object_set_new(reply, "session_id", json_integer(session_id));
json_object_set_new(reply, "transaction", json_string(transaction_text));
/* Send the success reply */
ret = ps_process_success(request, reply);
} else if (!strcasecmp(message_text, "hangup")) {
if(handle == NULL) {
/* Query is an handle-level command */
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_INVALID_REQUEST_PATH, "Unhandled request '%s' at this path", message_text);
goto jsondone;
}
if(handle->app == NULL || handle->app_handle == NULL) {
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_PLUGIN_DETACH, "No plugin attached");
goto jsondone;
}
janus_ice_webrtc_hangup(handle);
/* Prepare JSON reply */
json_t *reply = json_object();
json_object_set_new(reply, "janus", json_string("success"));
json_object_set_new(reply, "session_id", json_integer(session_id));
json_object_set_new(reply, "transaction", json_string(transaction_text));
/* Send the success reply */
ret = ps_process_success(request, reply);
} else if (!strcasecmp(message_text, "message")) {
if (handle == NULL) {
ret = ps_process_error (request, session_id, transaction_text, JANUS_ERROR_INVALID_REQUEST_PATH, "Unhandled request '%s' at this path", message_text);
goto jsondone;
}
if (handle->app == NULL || handle->app_handle == NULL) {
ret = ps_process_error (request, session_id, transaction_text, JANUS_ERROR_PLUGIN_MESSAGE, "No plugin to handle this message");
goto jsondone;
}
ps_plugin * plugin_t = (ps_plugin *) handle->app;
PS_LOG (LOG_VERB, "[%"SCNu64"] there is a message for %s\n", handle->handle_id, plugin_t->get_name());
json_t * body = json_object_get (root, "body");
if (body == NULL) {
ret = ps_process_error (request, session_id, transaction_text, JANUS_ERROR_MISSING_MANDATORY_ELEMENT, "Missing mandatory element (body)");
goto jsondone;
}
if (!json_is_object(body)) {
ret = ps_process_error (request, session_id, transaction_text, JANUS_ERROR_INVALID_JSON_OBJECT, "Invalid body object");
goto jsondone;
}
json_t * jsep = json_object_get (root, "jsep");
char * jsep_type = NULL;
char * jsep_sdp = NULL, * jsep_sdp_stripped = NULL;
if (jsep != NULL) {
if(!json_is_object(jsep)) {
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_INVALID_JSON_OBJECT, "Invalid jsep object");
goto jsondone;
}
json_t *type = json_object_get(jsep, "type");
if(!type) {
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_MISSING_MANDATORY_ELEMENT, "JSEP error: missing mandatory element (type)");
goto jsondone;
}
if(!json_is_string(type)) {
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_INVALID_ELEMENT_TYPE, "JSEP error: invalid element type (type should be a string)");
goto jsondone;
}
jsep_type = g_strdup(json_string_value(type));
type = NULL;
gboolean do_trickle = TRUE;
json_t *jsep_trickle = json_object_get(jsep, "trickle");
if(jsep_trickle && !json_is_boolean(jsep_trickle)) {
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_INVALID_ELEMENT_TYPE, "JSEP error: invalid element type (trickle should be a boolean)");
goto jsondone;
}
do_trickle = jsep_trickle ? json_is_true(jsep_trickle) : TRUE;
/* Are we still cleaning up from a previous media session? */
if(ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_CLEANING)) {
PS_LOG(LOG_VERB, "[%"SCNu64"] Still cleaning up from a previous media session, let's wait a bit...\n", handle->handle_id);
gint64 waited = 0;
while(ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_CLEANING)) {
g_usleep(100000);
waited += 100000;
if(waited >= 3*G_USEC_PER_SEC) {
PS_LOG(LOG_VERB, "[%"SCNu64"] -- Waited 3 seconds, that's enough!\n", handle->handle_id);
break;
}
}
}
/* Check the JSEP type */
ps_mutex_lock(&handle->mutex);
int offer = 0;
if(!strcasecmp(jsep_type, "offer")) {
offer = 1;
ps_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_PROCESSING_OFFER);
ps_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_GOT_OFFER);
ps_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_GOT_ANSWER);
} else if(!strcasecmp(jsep_type, "answer")) {
ps_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_GOT_ANSWER);
offer = 0;
} else {
/* TODO Handle other message types as well */
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_JSEP_UNKNOWN_TYPE, "JSEP error: unknown message type '%s'", jsep_type);
g_free(jsep_type);
ps_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_PROCESSING_OFFER);
ps_mutex_unlock(&handle->mutex);
goto jsondone;
}
json_t *sdp = json_object_get(jsep, "sdp");
if(!sdp) {
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_MISSING_MANDATORY_ELEMENT, "JSEP error: missing mandatory element (sdp)");
g_free(jsep_type);
ps_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_PROCESSING_OFFER);
ps_mutex_unlock(&handle->mutex);
goto jsondone;
}
if(!json_is_string(sdp)) {
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_INVALID_ELEMENT_TYPE, "JSEP error: invalid element type (sdp should be a string)");
g_free(jsep_type);
ps_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_PROCESSING_OFFER);
ps_mutex_unlock(&handle->mutex);
goto jsondone;
}
jsep_sdp = (char *)json_string_value(sdp);
PS_LOG(LOG_VERB, "[%"SCNu64"] Remote SDP:\n%s", handle->handle_id, jsep_sdp);
/* Is this valid SDP? */
int audio = 0, video = 0, data = 0, bundle = 0, rtcpmux = 0, trickle = 0;
janus_sdp *parsed_sdp = janus_sdp_preparse(jsep_sdp, &audio, &video, &data, &bundle, &rtcpmux, &trickle);
trickle = trickle && do_trickle;
if(parsed_sdp == NULL) {
/* Invalid SDP */
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_JSEP_INVALID_SDP, "JSEP error: invalid SDP");
g_free(jsep_type);
ps_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_PROCESSING_OFFER);
ps_mutex_unlock(&handle->mutex);
goto jsondone;
}
/* FIXME We're only handling single audio/video lines for now... */
PS_LOG(LOG_VERB, "[%"SCNu64"] Audio %s been negotiated, Video %s been negotiated, SCTP/DataChannels %s been negotiated\n",
handle->handle_id,
audio ? "has" : "has NOT",
video ? "has" : "has NOT",
data ? "have" : "have NOT");
if(audio > 1) {
PS_LOG(LOG_WARN, "[%"SCNu64"] More than one audio line? only going to negotiate one...\n", handle->handle_id);
}
if(video > 1) {
PS_LOG(LOG_WARN, "[%"SCNu64"] More than one video line? only going to negotiate one...\n", handle->handle_id);
}
if(data > 1) {
PS_LOG(LOG_WARN, "[%"SCNu64"] More than one data line? only going to negotiate one...\n", handle->handle_id);
}
PS_LOG(LOG_VERB, "[%"SCNu64"] The browser: %s BUNDLE, %s rtcp-mux, %s doing Trickle ICE\n", handle->handle_id,
bundle ? "supports" : "does NOT support",
rtcpmux ? "supports" : "does NOT support",
trickle ? "is" : "is NOT");
if(!ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_READY)
|| ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_ALERT)) {
/* New session */
if(offer) {
/* Setup ICE locally (we received an offer) */
if(janus_ice_setup_local(handle, offer, audio, video, data, bundle, rtcpmux, trickle) < 0) {
PS_LOG(LOG_ERR, "Error setting ICE locally\n");
g_free(jsep_type);
ps_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_PROCESSING_OFFER);
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_UNKNOWN, "Error setting ICE locally");
ps_mutex_unlock(&handle->mutex);
goto jsondone;
}
} else {
/* Make sure we're waiting for an ANSWER in the first place */
if(!handle->agent) {
PS_LOG(LOG_ERR, "Unexpected ANSWER (did we offer?)\n");
g_free(jsep_type);
ps_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_PROCESSING_OFFER);
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_UNEXPECTED_ANSWER, "Unexpected ANSWER (did we offer?)");
ps_mutex_unlock(&handle->mutex);
goto jsondone;
}
}
janus_sdp_parse(handle, parsed_sdp);
janus_sdp_free(parsed_sdp);
if(!offer) {
/* Set remote candidates now (we received an answer) */
if(bundle) {
ps_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_BUNDLE);
} else {
ps_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_BUNDLE);
}
if(rtcpmux) {
ps_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_RTCPMUX);
} else {
ps_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_RTCPMUX);
}
if(trickle) {
ps_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_TRICKLE);
} else {
ps_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_TRICKLE);
}
if(ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_BUNDLE)) {
PS_LOG(LOG_HUGE, "[%"SCNu64"] -- bundle is supported by the browser, getting rid of one of the RTP/RTCP components, if any...\n", handle->handle_id);
if(audio) {
/* Get rid of video and data, if present */
if(handle->streams && handle->video_stream) {
handle->audio_stream->video_ssrc = handle->video_stream->video_ssrc;
handle->audio_stream->video_ssrc_peer = handle->video_stream->video_ssrc_peer;
handle->audio_stream->video_ssrc_peer_rtx = handle->video_stream->video_ssrc_peer_rtx;
nice_agent_attach_recv(handle->agent, handle->video_stream->stream_id, 1, g_main_loop_get_context (handle->iceloop), NULL, NULL);
if(!janus_ice_is_rtcpmux_forced())
nice_agent_attach_recv(handle->agent, handle->video_stream->stream_id, 2, g_main_loop_get_context (handle->iceloop), NULL, NULL);
nice_agent_remove_stream(handle->agent, handle->video_stream->stream_id);
janus_ice_stream_free(handle->streams, handle->video_stream);
}
handle->video_stream = NULL;
handle->video_id = 0;
if(handle->streams && handle->data_stream) {
nice_agent_attach_recv(handle->agent, handle->data_stream->stream_id, 1, g_main_loop_get_context (handle->iceloop), NULL, NULL);
nice_agent_remove_stream(handle->agent, handle->data_stream->stream_id);
janus_ice_stream_free(handle->streams, handle->data_stream);
}
handle->data_stream = NULL;
handle->data_id = 0;
} else if(video) {
/* Get rid of data, if present */
if(handle->streams && handle->data_stream) {
nice_agent_attach_recv(handle->agent, handle->data_stream->stream_id, 1, g_main_loop_get_context (handle->iceloop), NULL, NULL);
nice_agent_remove_stream(handle->agent, handle->data_stream->stream_id);
janus_ice_stream_free(handle->streams, handle->data_stream);
}
handle->data_stream = NULL;
handle->data_id = 0;
}
}
if(ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_RTCPMUX) && !janus_ice_is_rtcpmux_forced()) {
PS_LOG(LOG_HUGE, "[%"SCNu64"] -- rtcp-mux is supported by the browser, getting rid of RTCP components, if any...\n", handle->handle_id);
if(handle->audio_stream && handle->audio_stream->components != NULL) {
nice_agent_attach_recv(handle->agent, handle->audio_id, 2, g_main_loop_get_context (handle->iceloop), NULL, NULL);
/* Free the component */
janus_ice_component_free(handle->audio_stream->components, handle->audio_stream->rtcp_component);
handle->audio_stream->rtcp_component = NULL;
/* Create a dummy candidate and enforce it as the one to use for this now unneeded component */
NiceCandidate *c = nice_candidate_new(NICE_CANDIDATE_TYPE_HOST);
c->component_id = 2;
c->stream_id = handle->audio_stream->stream_id;
#ifndef HAVE_LIBNICE_TCP
c->transport = NICE_CANDIDATE_TRANSPORT_UDP;
#endif
strncpy(c->foundation, "1", NICE_CANDIDATE_MAX_FOUNDATION);
c->priority = 1;
nice_address_set_from_string(&c->addr, "127.0.0.1");
nice_address_set_port(&c->addr, janus_ice_get_rtcpmux_blackhole_port());
c->username = g_strdup(handle->audio_stream->ruser);
c->password = g_strdup(handle->audio_stream->rpass);
if(!nice_agent_set_selected_remote_candidate(handle->agent, handle->audio_stream->stream_id, 2, c)) {
PS_LOG(LOG_ERR, "[%"SCNu64"] Error forcing dummy candidate on RTCP component of stream %d\n", handle->handle_id, handle->audio_stream->stream_id);
nice_candidate_free(c);
}
}
if(handle->video_stream && handle->video_stream->components != NULL) {
nice_agent_attach_recv(handle->agent, handle->video_id, 2, g_main_loop_get_context (handle->iceloop), NULL, NULL);
/* Free the component */
janus_ice_component_free(handle->video_stream->components, handle->video_stream->rtcp_component);
handle->video_stream->rtcp_component = NULL;
/* Create a dummy candidate and enforce it as the one to use for this now unneeded component */
NiceCandidate *c = nice_candidate_new(NICE_CANDIDATE_TYPE_HOST);
c->component_id = 2;
c->stream_id = handle->video_stream->stream_id;
#ifndef HAVE_LIBNICE_TCP
c->transport = NICE_CANDIDATE_TRANSPORT_UDP;
#endif
strncpy(c->foundation, "1", NICE_CANDIDATE_MAX_FOUNDATION);
c->priority = 1;
nice_address_set_from_string(&c->addr, "127.0.0.1");
nice_address_set_port(&c->addr, janus_ice_get_rtcpmux_blackhole_port());
c->username = g_strdup(handle->video_stream->ruser);
c->password = g_strdup(handle->video_stream->rpass);
if(!nice_agent_set_selected_remote_candidate(handle->agent, handle->video_stream->stream_id, 2, c)) {
PS_LOG(LOG_ERR, "[%"SCNu64"] Error forcing dummy candidate on RTCP component of stream %d\n", handle->handle_id, handle->video_stream->stream_id);
nice_candidate_free(c);
}
}
}
/* FIXME Any disabled m-line? */
if(strstr(jsep_sdp, "m=audio 0")) {
PS_LOG(LOG_VERB, "[%"SCNu64"] Audio disabled via SDP\n", handle->handle_id);
if(!ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_BUNDLE)
|| (!video && !data)) {
PS_LOG(LOG_HUGE, " -- Marking audio stream as disabled\n");
janus_ice_stream *stream = g_hash_table_lookup(handle->streams, GUINT_TO_POINTER(handle->audio_id));
if(stream)
stream->disabled = TRUE;
}
}
if(strstr(jsep_sdp, "m=video 0")) {
PS_LOG(LOG_VERB, "[%"SCNu64"] Video disabled via SDP\n", handle->handle_id);
if(!ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_BUNDLE)
|| (!audio && !data)) {
PS_LOG(LOG_HUGE, " -- Marking video stream as disabled\n");
janus_ice_stream *stream = NULL;
if(!ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_BUNDLE)) {
stream = g_hash_table_lookup(handle->streams, GUINT_TO_POINTER(handle->video_id));
} else {
gint id = handle->audio_id > 0 ? handle->audio_id : handle->video_id;
stream = g_hash_table_lookup(handle->streams, GUINT_TO_POINTER(id));
}
if(stream)
stream->disabled = TRUE;
}
}
if(strstr(jsep_sdp, "m=application 0 DTLS/SCTP")) {
PS_LOG(LOG_VERB, "[%"SCNu64"] Data Channel disabled via SDP\n", handle->handle_id);
if(!ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_BUNDLE)
|| (!audio && !video)) {
PS_LOG(LOG_HUGE, " -- Marking data channel stream as disabled\n");
janus_ice_stream *stream = NULL;
if(!ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_BUNDLE)) {
stream = g_hash_table_lookup(handle->streams, GUINT_TO_POINTER(handle->data_id));
} else {
gint id = handle->audio_id > 0 ? handle->audio_id : (handle->video_id > 0 ? handle->video_id : handle->data_id);
stream = g_hash_table_lookup(handle->streams, GUINT_TO_POINTER(id));
}
if(stream)
stream->disabled = TRUE;
}
}
/* We got our answer */
ps_flags_clear(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_PROCESSING_OFFER);
/* Any pending trickles? */
if(handle->pending_trickles) {
PS_LOG(LOG_VERB, "[%"SCNu64"] -- Processing %d pending trickle candidates\n", handle->handle_id, g_list_length(handle->pending_trickles));
GList *temp = NULL;
while(handle->pending_trickles) {
temp = g_list_first(handle->pending_trickles);
handle->pending_trickles = g_list_remove_link(handle->pending_trickles, temp);
janus_ice_trickle *trickle = (janus_ice_trickle *)temp->data;
g_list_free(temp);
if(trickle == NULL)
continue;
if((ps_get_monotonic_time() - trickle->received) > 15*G_USEC_PER_SEC) {
/* FIXME Candidate is too old, discard it */
janus_ice_trickle_destroy(trickle);
/* FIXME We should report that */
continue;
}
json_t *candidate = trickle->candidate;
if(candidate == NULL) {
janus_ice_trickle_destroy(trickle);
continue;
}
if(json_is_object(candidate)) {
/* We got a single candidate */
int error = 0;
const char *error_string = NULL;
if((error = janus_ice_trickle_parse(handle, candidate, &error_string)) != 0) {
/* FIXME We should report the error parsing the trickle candidate */
}
} else if(json_is_array(candidate)) {
/* We got multiple candidates in an array */
PS_LOG(LOG_VERB, "Got multiple candidates (%zu)\n", json_array_size(candidate));
if(json_array_size(candidate) > 0) {
/* Handle remote candidates */
size_t i = 0;
for(i=0; i<json_array_size(candidate); i++) {
json_t *c = json_array_get(candidate, i);
/* FIXME We don't care if any trickle fails to parse */
janus_ice_trickle_parse(handle, c, NULL);
}
}
}
/* Done, free candidate */
janus_ice_trickle_destroy(trickle);
}
}
/* This was an answer, check if it's time to start ICE */
if(ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_TRICKLE) &&
!ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_ALL_TRICKLES)) {
PS_LOG(LOG_VERB, "[%"SCNu64"] -- ICE Trickling is supported by the browser, waiting for remote candidates...\n", handle->handle_id);
ps_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_START);
} else {
PS_LOG(LOG_VERB, "[%"SCNu64"] Done! Sending connectivity checks...\n", handle->handle_id);
if(handle->audio_id > 0) {
janus_ice_setup_remote_candidates(handle, handle->audio_id, 1);
if(!ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_RTCPMUX)) /* http://tools.ietf.org/html/rfc5761#section-5.1.3 */
janus_ice_setup_remote_candidates(handle, handle->audio_id, 2);
}
if(handle->video_id > 0) {
janus_ice_setup_remote_candidates(handle, handle->video_id, 1);
if(!ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_RTCPMUX)) /* http://tools.ietf.org/html/rfc5761#section-5.1.3 */
janus_ice_setup_remote_candidates(handle, handle->video_id, 2);
}
if(handle->data_id > 0) {
janus_ice_setup_remote_candidates(handle, handle->data_id, 1);
}
}
}
} else {
/* TODO Actually handle session updates: for now we ignore them, and just relay them to plugins */
PS_LOG(LOG_WARN, "[%"SCNu64"] Ignoring negotiation update, we don't support them yet...\n", handle->handle_id);
}
handle->remote_sdp = g_strdup (jsep_sdp);
ps_mutex_unlock (&handle->mutex);
/* Anonymize SDP */
jsep_sdp_stripped = janus_sdp_anonymize (jsep_sdp);
if (jsep_sdp_stripped == NULL) {
ret = ps_process_error (request, session_id, transaction_text, JANUS_ERROR_JSEP_INVALID_SDP, "JSEP Error: invalid SDP");
g_free (jsep_type);
ps_flags_clear (&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_PROCESSING_OFFER);
goto jsondone;
}
sdp = NULL;
ps_flags_clear (&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_PROCESSING_OFFER);
}
if (handle->app == NULL || handle->app_handle == NULL || !ps_plugin_session_is_alive (handle->app_handle)) {
PS_LOG (LOG_HUGE, "session alive %d\n", ps_plugin_session_is_alive (handle->app_handle));
ret = ps_process_error (request, session_id, transaction_text, JANUS_ERROR_PLUGIN_MESSAGE, "no plugin to handle this message");
if (jsep_type) g_free (jsep_type);
if (jsep_sdp_stripped) g_free (jsep_sdp_stripped);
ps_flags_clear (&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_PROCESSING_OFFER);
goto jsondone;
}
/* Send the message to the plugin, should eventually free transaction_text, body_text, jsep_type and sdp */
char * body_text = json_dumps (body, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
ps_plugin_result * result = plugin_t->handle_message (handle->app_handle, g_strdup((char *) transaction_text), body_text, jsep_type, jsep_sdp_stripped);
if (result == NULL) {
ret = ps_process_error (request, session_id, transaction_text, JANUS_ERROR_PLUGIN_MESSAGE, "Plugin didn't give a result");
goto jsondone;
}
if (result->type == PS_PLUGIN_OK) {
if (result->content == NULL) {
ret = ps_process_error (request, session_id, transaction_text, JANUS_ERROR_PLUGIN_MESSAGE, "Plugin didn't provide any content for this synchronous response");
ps_plugin_result_destroy (result);
goto jsondone;
}
json_error_t error;
json_t * event = json_loads (result->content, 0, &error);
if(!event) {
PS_LOG(LOG_ERR, "[%"SCNu64"] Cannot send response from plugin (JSON error: on line %d: %s)\n", handle->handle_id, error.line, error.text);
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_PLUGIN_MESSAGE, "Plugin returned an invalid JSON response");
ps_plugin_result_destroy(result);
goto jsondone;
}
if(!json_is_object(event)) {
PS_LOG(LOG_ERR, "[%"SCNu64"] Cannot send response from plugin (JSON error: not an object)\n", handle->handle_id);
json_decref(event);
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_PLUGIN_MESSAGE, "Plugin returned an invalid JSON response");
ps_plugin_result_destroy(result);
goto jsondone;
}
/* Prepare JSON response */
json_t *reply = json_object();
json_object_set_new(reply, "janus", json_string("success"));
json_object_set_new(reply, "session_id", json_integer(session->session_id));
json_object_set_new(reply, "sender", json_integer(handle->handle_id));
json_object_set_new(reply, "transaction", json_string(transaction_text));
json_t *plugin_data = json_object();
json_object_set_new(plugin_data, "plugin", json_string(plugin_t->get_package()));
json_object_set_new(plugin_data, "data", event);
json_object_set_new(reply, "plugindata", plugin_data);
/* Send the success reply */
ret = ps_process_success(request, reply);
} else if (result->type == PS_PLUGIN_OK_WAIT) {
/* The plugin received the request but didn't process it yet, send an ack (asynchronous notifications may follow) */
json_t *reply = json_object();
json_object_set_new(reply, "janus", json_string("ack"));
json_object_set_new(reply, "session_id", json_integer(session_id));
if(result->content)
json_object_set_new(reply, "hint", json_string(result->content));
json_object_set_new(reply, "transaction", json_string(transaction_text));
/* Send the success reply */
ret = ps_process_success(request, reply);
} else {
/* Something went horribly wrong! */
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_PLUGIN_MESSAGE, "%s", result->content ? g_strdup(result->content) : "Plugin returned a severe (unknown) error");
ps_plugin_result_destroy(result);
goto jsondone;
}
ps_plugin_result_destroy (result);
} else if (!strcasecmp(message_text, "trickle")) {
if(handle == NULL) {
/* Trickle is an handle-level command */
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_INVALID_REQUEST_PATH, "Unhandled request '%s' at this path", message_text);
goto jsondone;
}
if(handle->app == NULL || handle->app_handle == NULL || !ps_plugin_session_is_alive(handle->app_handle)) {
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_PLUGIN_MESSAGE, "No plugin to handle this trickle candidate");
goto jsondone;
}
json_t *candidate = json_object_get(root, "candidate");
json_t *candidates = json_object_get(root, "candidates");
if(candidate == NULL && candidates == NULL) {
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_MISSING_MANDATORY_ELEMENT, "Missing mandatory element (candidate|candidates)");
goto jsondone;
}
if(candidate != NULL && candidates != NULL) {
ret = ps_process_error(request, session_id, transaction_text, JANUS_ERROR_INVALID_JSON, "Can't have both candidate and candidates");
goto jsondone;
}
ps_mutex_lock(&handle->mutex);
if(!ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_TRICKLE)) {
/* It looks like this peer supports Trickle, after all */
PS_LOG(LOG_VERB, "Handle %"SCNu64" supports trickle even if it didn't negotiate it...\n", handle->handle_id);
ps_flags_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_TRICKLE);
}
/* Is there any stream ready? this trickle may get here before the SDP it relates to */
if(handle->audio_stream == NULL && handle->video_stream == NULL && handle->data_stream == NULL) {
PS_LOG(LOG_WARN, "[%"SCNu64"] No stream, queueing this trickle as it got here before the SDP...\n", handle->handle_id);
/* Enqueue this trickle candidate(s), we'll process this later */
janus_ice_trickle *early_trickle = janus_ice_trickle_new(handle, transaction_text, candidate ? candidate : candidates);
handle->pending_trickles = g_list_append(handle->pending_trickles, early_trickle);
/* Send the ack right away, an event will tell the application if the candidate(s) failed */
goto trickledone;
}
/* Is the ICE stack ready already? */
if(ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_PROCESSING_OFFER) ||
!ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_GOT_OFFER) ||
!ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_GOT_ANSWER)) {
const char *cause = NULL;
if(ps_flags_is_set(&handle->webrtc_flags, JANUS_ICE_HANDLE_WEBRTC_PROCESSING_OFFER))
cause = "processing the offer";