Statistics
| Branch: | Revision:

janus-gateway / events.c @ 1d31e31f

History | View | Annotate | Download (4.15 KB)

1
/*! \file    events.c
2
 * \author   Lorenzo Miniero <lorenzo@meetecho.com>
3
 * \copyright GNU General Public License v3
4
 * \brief    Event handler notifications
5
 * \details  Event handler plugins can receive events from the Janus core
6
 * and other plugins, in order to handle them somehow. This methods
7
 * provide helpers to notify events to such handlers. 
8
 * 
9
 * \ingroup core
10
 * \ref core
11
 */
12
 
13
#include <stdarg.h>
14

    
15
#include "events.h"
16
#include "utils.h"
17

    
18
static gboolean eventsenabled = FALSE;
19
static GHashTable *eventhandlers = NULL;
20
void janus_events_init(gboolean enabled, GHashTable *handlers) {
21
        eventsenabled = enabled;
22
        eventhandlers = handlers;
23
}
24

    
25
gboolean janus_events_is_enabled(void) {
26
        return eventsenabled;
27
}
28

    
29
void janus_events_notify_handlers(int type, guint64 session_id, ...) {
30
        if(!eventsenabled || eventhandlers == NULL || g_hash_table_size(eventhandlers) == 0)
31
                return;
32

    
33
        /* Prepare the event to notify as a Jansson json_t object */
34
        json_t *event = json_object();
35
        json_object_set_new(event, "type", json_integer(type));
36
        json_object_set_new(event, "timestamp", json_integer(janus_get_monotonic_time()));
37
        json_object_set_new(event, "session_id", json_integer(session_id));
38
        json_t *body = NULL;
39
        if(type != JANUS_EVENT_TYPE_WEBRTC)
40
                body = json_object();
41

    
42
        /* Each type may require different arguments */
43
        va_list args;
44
        va_start(args, session_id);
45
        switch(type) {
46
                case JANUS_EVENT_TYPE_SESSION: {
47
                        /* For sessions, there's just a generic event name (what happened) */
48
                        char *name = va_arg(args, char *);
49
                        json_object_set_new(body, "name", json_string(name));
50
                        break;
51
                }
52
                case JANUS_EVENT_TYPE_HANDLE: {
53
                        /* For handles, there's the handle ID and a generic event name (what happened) */
54
                        guint64 handle_id = va_arg(args, guint64);
55
                        json_object_set_new(event, "handle_id", json_integer(handle_id));
56
                        char *name = va_arg(args, char *);
57
                        json_object_set_new(body, "name", json_string(name));
58
                        break;
59
                }
60
                case JANUS_EVENT_TYPE_JSEP: {
61
                        /* For JSEP-related events, there's the handle ID, whether the SDP is local or remote, the JSEP type and the SDP itself */
62
                        guint64 handle_id = va_arg(args, guint64);
63
                        json_object_set_new(event, "handle_id", json_integer(handle_id));
64
                        char *owner = va_arg(args, char *);
65
                        json_object_set_new(body, "owner", json_string(owner));
66
                        json_t *jsep = json_object();
67
                        char *sdp_type = va_arg(args, char *);
68
                        json_object_set_new(jsep, "type", json_string(sdp_type));
69
                        char *sdp = va_arg(args, char *);
70
                        json_object_set_new(jsep, "sdp", json_string(sdp));
71
                        json_object_set_new(body, "jsep", jsep);
72
                        break;
73
                }
74
                case JANUS_EVENT_TYPE_WEBRTC:
75
                case JANUS_EVENT_TYPE_MEDIA: {
76
                        /* For WebRTC and media-related events, there's the handle ID and a json_t object with info on what happened */
77
                        guint64 handle_id = va_arg(args, guint64);
78
                        json_object_set_new(event, "handle_id", json_integer(handle_id));
79
                        /* The body is what we get from the event */
80
                        body = va_arg(args, json_t *);
81
                        break;
82
                }
83
                case JANUS_EVENT_TYPE_PLUGIN:
84
                case JANUS_EVENT_TYPE_TRANSPORT: {
85
                        /* For plugin-originated events, there's the handle ID, the plugin name, and a generic, plugin specific, json_t object */
86
                        guint64 handle_id = va_arg(args, guint64);
87
                        json_object_set_new(event, "handle_id", json_integer(handle_id));
88
                        char *name = va_arg(args, char *);
89
                        json_object_set_new(body, "plugin", json_string(name));
90
                        json_t *data = va_arg(args, json_t *);
91
                        json_object_set(body, "data", data);
92
                        break;
93
                }
94
                default:
95
                        JANUS_LOG(LOG_WARN, "Unknown event type '%d'\n", type);
96
                        json_decref(event);
97
                        json_decref(body);
98
                        return;
99
        }
100
        json_object_set_new(event, "event", body);
101
        va_end(args);
102

    
103
        /* Notify all interested handlers, increasing the event reference to make sure it's not lost because of errors */
104
        GHashTableIter iter;
105
        gpointer value;
106
        g_hash_table_iter_init(&iter, eventhandlers);
107
        json_incref(event);
108
        while(g_hash_table_iter_next(&iter, NULL, &value)) {
109
                janus_eventhandler *e = value;
110
                if(e == NULL)
111
                        continue;
112
                if(!janus_flags_is_set(&e->events_mask, type))
113
                        continue;
114
                e->incoming_event(event);
115
        }
116
        json_decref(event);
117
        /* Unref the final event reference, interested handlers will have their own reference */
118
        json_decref(event);
119
}