/
eventpipe_events.c
218 lines (186 loc) · 7.66 KB
/
eventpipe_events.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
#include "eventpipe.h"
static struct globals_t globals;
static struct eventpipe_call *head;
void eventpipe_events_track_call(switch_core_session_t *session) {
struct eventpipe_call *call;
struct eventpipe_call *current = NULL;
switch_assert(session != NULL);
if (!(call = switch_core_session_alloc(session, sizeof(*call)))) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Memory Error\n");
return;
}
call->session = session;
call->next = NULL;
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG,
"Tracking call events\n");
switch_mutex_lock(globals.eventpipe_call_list_mutex);
if (!head) {
head = call;
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG,
"Track call events (first one)\n");
} else {
current = head;
call->next = current;
head = call;
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG,
"Track call events\n");
}
switch_mutex_unlock(globals.eventpipe_call_list_mutex);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG,
"Tracking call events done\n");
}
void eventpipe_events_untrack_call(switch_core_session_t *session) {
struct eventpipe_call *current, *next, *last;
next = NULL;
last = NULL;
current = NULL;
switch_assert(session != NULL);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG,
"Untracking call events\n");
switch_mutex_lock(globals.eventpipe_call_list_mutex);
if (head) {
if (head->session == session) {
head = NULL;
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_NOTICE, "No more call to track\n");
goto eventpipe_remove_call_end;
}
} else {
goto eventpipe_remove_call_end;
}
current = head;
last = head;
while (current) {
if (current->session == session) {
next = current->next;
last->next = next;
goto eventpipe_remove_call_end;
} else {
current = current->next;
}
last = current;
}
eventpipe_remove_call_end:
switch_mutex_unlock(globals.eventpipe_call_list_mutex);
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG,
"Untracking call events done\n");
}
void eventpipe_events_on_dtmf(switch_core_session_t *session, switch_event_t *event) {
char args[8192];
const char *dtmf_digit = switch_str_nil(switch_event_get_header(event, "dtmf-digit"));
switch_channel_t *channel = switch_core_session_get_channel(session);
const char *hangup_on_star = switch_str_nil(switch_channel_get_variable(channel, "eventpipe_conference_hangup_on_star"));
const char *conf_room = switch_str_nil(switch_channel_get_variable(channel, "eventpipe_conference_room"));
const char *conf_member_id = switch_str_nil(switch_channel_get_variable(channel, "eventpipe_conference_member_id"));
/* handle conference kick if channel is in conference and digit '*' was pressed */
if ((!strcmp(hangup_on_star, "true")) && (!strcmp(dtmf_digit, "*")) && (!zstr(conf_room)) && (!zstr(conf_member_id))) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO,
"Conference, dtmf '*' pressed, kick member %s from room %s\n",
conf_member_id, conf_room);
switch_snprintf(args, sizeof(args), "%s kick %s", conf_room, conf_member_id);
eventpipe_execute_api(session, "conference", args);
}
}
void eventpipe_events_on_conference(switch_core_session_t *session, switch_event_t *event) {
char args[8192];
char record_args[8192];
const char *action = switch_str_nil(switch_event_get_header(event, "action"));
const char *member_id = switch_str_nil(switch_event_get_header(event, "member-id"));
const char *conf_room = switch_str_nil(switch_event_get_header(event, "conference-name"));
const char *calluuid = switch_core_session_get_uuid(session);
switch_channel_t *channel = switch_core_session_get_channel(session);
if (!channel) {
return;
}
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG,
"Event-Name: %s, Event-Subclass: %s, Conference-Name: %s, Action: %s, Member-ID: %s, Unique-ID: %s\n",
switch_str_nil(switch_event_get_header(event, "event-name")),
switch_str_nil(switch_event_get_header(event, "event-subclass")),
conf_room, action, member_id, calluuid);
if (!strcmp(action, "add-member")) {
const char *enter_sound = switch_str_nil(switch_channel_get_variable(channel, "eventpipe_conference_enter_sound"));
const char *record_file = switch_str_nil(switch_channel_get_variable(channel, "eventpipe_conference_record_file"));
switch_channel_set_variable(channel, "eventpipe_conference_member_id", member_id);
switch_channel_set_variable(channel, "eventpipe_conference_room", conf_room);
if (!zstr(enter_sound)) {
switch_snprintf(args, sizeof(args), "%s play %s async",
conf_room, enter_sound);
eventpipe_execute_api(session, "conference", args);
}
if (!zstr(record_file)) {
switch_snprintf(record_args, sizeof(record_args), "%s record %s",
conf_room, record_file);
eventpipe_execute_api(session, "conference", record_args);
}
} else if (!strcmp(action, "del-member")) {
const char *exit_sound = switch_str_nil(switch_channel_get_variable(channel, "eventpipe_conference_exit_sound"));
switch_channel_set_variable(channel, "eventpipe_conference_member_id", "");
switch_channel_set_variable(channel, "eventpipe_conference_room", "");
switch_channel_set_variable(channel, "eventpipe_conference_record_file", "");
if (!zstr(exit_sound)) {
switch_snprintf(args, sizeof(args), "conference %s play %s async",
conf_room, exit_sound);
eventpipe_execute_api(session, "conference", args);
}
}
}
void eventpipe_events_handler(switch_event_t *event)
{
struct eventpipe_call *current = NULL;
const char *uuid1 = "";
const char *uuid2 = "";
switch_assert(event != NULL);
if ((event->event_id != SWITCH_EVENT_CUSTOM) && (event->event_id != SWITCH_EVENT_DTMF)) {
return;
}
uuid1 = switch_event_get_header(event, "unique-id");
if (zstr(uuid1)) {
return;
}
switch_mutex_lock(globals.eventpipe_call_list_mutex);
if (!head) {
goto eventpipe_events_handler_done;
}
if (!head->session) {
goto eventpipe_events_handler_done;
}
current = head;
while (current) {
if (current->session) {
uuid2 = "";
uuid2 = switch_core_session_get_uuid(current->session);
/* check if event unique-id is matching a call uuid in eventpipe */
if ((uuid2) && (!zstr(uuid2) && (!strcmp(uuid1, uuid2)))) {
/* conference event case */
if (event->event_id == SWITCH_EVENT_CUSTOM) {
const char *event_subclass = switch_str_nil(switch_event_get_header(event, "event-subclass"));
if (!strcmp(event_subclass, PLIVO_EVENT_CONFERENCE)) {
eventpipe_events_on_conference(current->session, event);
}
/* dtmf event case */
} else if (event->event_id == SWITCH_EVENT_DTMF) {
eventpipe_events_on_dtmf(current->session, event);
}
goto eventpipe_events_handler_done;
}
}
current = current->next;
}
eventpipe_events_handler_done:
switch_mutex_unlock(globals.eventpipe_call_list_mutex);
}
int eventpipe_events_load(const char *modname, switch_memory_pool_t *pool) {
memset(&globals, 0, sizeof(globals));
head = NULL;
switch_mutex_init(&globals.eventpipe_call_list_mutex, SWITCH_MUTEX_NESTED, pool);
if (switch_event_bind_removable(modname, SWITCH_EVENT_ALL, SWITCH_EVENT_SUBCLASS_ANY,
eventpipe_events_handler, NULL, &globals.node) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Couldn't bind!\n");
return SWITCH_STATUS_GENERR;
}
return SWITCH_STATUS_SUCCESS;
}
void eventpipe_events_unload() {
if (&globals.node) {
switch_event_unbind(&globals.node);
}
}