-
Notifications
You must be signed in to change notification settings - Fork 3
/
mod_simpleamd.c
455 lines (408 loc) · 15.6 KB
/
mod_simpleamd.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
/*
* mod_simpleamd for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2014-2015, Christopher M. Rienzo <[email protected]>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Christopher M. Rienzo <[email protected]>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Christopher M. Rienzo <[email protected]>
*
* Maintainer: Christopher M. Rienzo <[email protected]>
*
* mod_simpleamd.c -- Crude answering machine detector using a simple energy threshold for VAD
*
*/
#include <switch.h>
#include <simpleamd.h>
/* Defines module interface to FreeSWITCH */
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_simpleamd_shutdown);
SWITCH_MODULE_LOAD_FUNCTION(mod_simpleamd_load);
SWITCH_MODULE_DEFINITION(mod_simpleamd, mod_simpleamd_load, mod_simpleamd_shutdown, NULL);
#define SIMPLEVAD_START_SYNTAX "{threshold_adjust_ms=200,max_threshold=1300,threshold=130,voice_ms=60,voice_end_ms=850}"
#define SIMPLEVAD_STOP_SYNTAX ""
#define SIMPLEAMD_START_SYNTAX "{wait_for_voice_ms=2000,machine_ms=1300,threshold_adjust_ms=200,max_threshold=1300,threshold=130,voice_ms=60,voice_end_ms=850}"
#define SIMPLEAMD_STOP_SYNTAX ""
#define SIMPLEBEEP_START_SYNTAX ""
#define SIMPLEBEEP_STOP_SYNTAX ""
/**
* Forward logs
* @param level
* @param file that sent the log
* @param line number that sent the log
* @param user_data
* @param message log message
*/
static void log_handler(samd_log_level_t level, void *user_data, const char *file, int line, const char *message)
{
switch_log_level_t slevel = SWITCH_LOG_DEBUG;
switch(level) {
case SAMD_LOG_DEBUG:
slevel = SWITCH_LOG_DEBUG10;
break;
case SAMD_LOG_INFO:
slevel = SWITCH_LOG_INFO;
break;
case SAMD_LOG_WARNING:
slevel = SWITCH_LOG_WARNING;
break;
case SAMD_LOG_ERROR:
slevel = SWITCH_LOG_ERROR;
break;
}
switch_log_printf(SWITCH_CHANNEL_ID_LOG, file, "", line, switch_core_session_get_uuid((switch_core_session_t *)user_data), slevel, "%s\n", message);
}
/**
* Forward VAD events
*
* @param vad_event the event fired
* @param time_ms time since beginning of detection
* @param user_data
*/
static void vad_event_handler(samd_vad_event_t vad_event, uint32_t time_ms, uint32_t total_voice_ms, uint32_t transition_ms, void *user_data)
{
if (vad_event == SAMD_VAD_VOICE_BEGIN || vad_event == SAMD_VAD_SILENCE_BEGIN) {
switch_event_t *event = NULL;
switch_core_session_t *session = (switch_core_session_t *)user_data;
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Processing VAD event, %s\n", samd_vad_event_to_string(vad_event));
if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, "simpleamd::vad") == SWITCH_STATUS_SUCCESS) {
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Value", "%s", samd_vad_event_to_string(vad_event));
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "time-ms", "%d", time_ms);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Unique-ID", switch_core_session_get_uuid(session));
switch_channel_event_set_data(switch_core_session_get_channel(session), event);
switch_event_fire(&event);
}
}
}
/**
* Process a buffer of audio data for VAD events
*
* @param bug the session's media bug
* @param user_data the detector
* @param type the type of data available from the bug
* @return SWITCH_TRUE
*/
static switch_bool_t vad_process_buffer(switch_media_bug_t *bug, void *user_data, switch_abc_type_t type)
{
samd_vad_t *vad = (samd_vad_t *)user_data;
switch(type) {
case SWITCH_ABC_TYPE_INIT: {
switch_core_session_t *session = switch_core_media_bug_get_session(bug);
switch_codec_implementation_t read_impl = { 0 };
switch_core_session_get_read_impl(session, &read_impl);
samd_vad_set_sample_rate(vad, read_impl.actual_samples_per_second);
break;
}
case SWITCH_ABC_TYPE_READ_REPLACE:
{
switch_frame_t *frame;
if ((frame = switch_core_media_bug_get_read_replace_frame(bug))) {
samd_vad_process_buffer(vad, frame->data, frame->samples, frame->channels);
switch_core_media_bug_set_read_replace_frame(bug, frame);
}
break;
}
case SWITCH_ABC_TYPE_CLOSE:
samd_vad_destroy(&vad);
break;
default:
break;
}
return SWITCH_TRUE;
}
static void configure_vad(samd_vad_t *vad, switch_core_session_t *session, switch_event_t *params)
{
const char *val;
if ((val = switch_event_get_header(params, "threshold_adjust_ms"))) {
int v;
if (switch_is_number(val) && (v = atoi(val)) >= 0) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "threshold_adjust_ms = %d\n", v);
samd_vad_set_initial_adjust_ms(vad, v);
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Ignoring invalid threshold_adjust_ms = \"%s\"\n", val);
}
}
if ((val = switch_event_get_header(params, "max_threshold"))) {
double v;
if (switch_is_number(val) && (v = atof(val)) > 0.0 && v < 32767.0) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "max_threshold = %f\n", v);
samd_vad_set_max_energy_threshold(vad, v);
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Ignoring invalid max_threshold = \"%s\"\n", val);
}
}
if ((val = switch_event_get_header(params, "threshold"))) {
double v;
if (switch_is_number(val) && (v = atof(val)) > 0.0 && v < 32767.0) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "threshold = %f\n", v);
samd_vad_set_energy_threshold(vad, v);
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Ignoring invalid threshold = \"%s\"\n", val);
}
}
if ((val = switch_event_get_header(params, "voice_ms"))) {
int v;
if (switch_is_number(val) && (v = atoi(val)) > 0) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "voice_ms = %d\n", v);
samd_vad_set_voice_ms(vad, v);
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Ignoring invalid voice_ms = \"%s\"\n", val);
}
}
if ((val = switch_event_get_header(params, "voice_end_ms"))) {
int v;
if (switch_is_number(val) && (v = atoi(val)) > 0) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "voice_end_ms = %d\n", v);
samd_vad_set_voice_end_ms(vad, v);
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Ignoring invalid voice_end_ms = \"%s\"\n", val);
}
}
}
static samd_vad_t *create_vad(switch_core_session_t *session, const char *args)
{
samd_vad_t *vad = NULL;
/* Create detector */
samd_vad_init(&vad);
samd_vad_set_log_handler(vad, log_handler, session);
samd_vad_set_event_handler(vad, vad_event_handler, session);
/* configure detector */
if (!zstr(args)) {
switch_event_t *params = NULL;
char *largs = switch_core_session_strdup(session, args);
if (switch_event_create_brackets(largs, '{', '}', ',', ¶ms, &largs, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
configure_vad(vad, session, params);
}
if (params) {
switch_event_destroy(¶ms);
}
}
return vad;
}
/**
* APP interface to start VAD
*/
SWITCH_STANDARD_APP(simplevad_start_app)
{
switch_channel_t *channel = switch_core_session_get_channel(session);
switch_media_bug_t *bug = NULL;
samd_vad_t *vad = NULL;
/* are we already running? */
bug = switch_channel_get_private(channel, "_mod_simpleamd_vad");
if (bug) {
switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "-ERR already running");
return;
}
vad = create_vad(session, data);
/* Add media bug */
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Starting VAD\n");
switch_core_media_bug_add(session, "_mod_simpleamd_vad", NULL, vad_process_buffer, vad, 0, SMBF_READ_REPLACE | SMBF_NO_PAUSE, &bug);
if (!bug) {
samd_vad_destroy(&vad);
switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "-ERR can't create media bug");
return;
}
switch_channel_set_private(channel, "_mod_simpleamd_vad", bug);
switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "+OK started");
}
/**
* APP interface to stop VAD
*/
SWITCH_STANDARD_APP(simplevad_stop_app)
{
switch_channel_t *channel = switch_core_session_get_channel(session);
switch_media_bug_t *bug = switch_channel_get_private(channel, "_mod_simpleamd_vad");
if (bug) {
switch_core_media_bug_remove(session, &bug);
switch_channel_set_private(channel, "_mod_simpleamd_vad", NULL);
switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "+OK stopped");
} else {
switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "-ERR not running");
}
}
/**
* Forward AMD events
*
* @param vad_event
* @param samples
* @param user_data
*/
static void amd_event_handler(samd_event_t amd_event, uint32_t samples, void *user_data)
{
switch_event_t *event = NULL;
switch_core_session_t *session = (switch_core_session_t *)user_data;
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Processing AMD event, %s\n", samd_event_to_string(amd_event));
if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, "simpleamd::amd") == SWITCH_STATUS_SUCCESS) {
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Value", "%s", samd_event_to_string(amd_event));
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Unique-ID", switch_core_session_get_uuid(session));
switch_channel_event_set_data(switch_core_session_get_channel(session), event);
switch_event_fire(&event);
}
}
/**
* Process a buffer of audio data for AMD events
*
* @param bug the session's media bug
* @param user_data the detector
* @param type the type of data available from the bug
* @return SWITCH_TRUE
*/
static switch_bool_t amd_process_buffer(switch_media_bug_t *bug, void *user_data, switch_abc_type_t type)
{
samd_t *amd = (samd_t *)user_data;
switch(type) {
case SWITCH_ABC_TYPE_INIT: {
switch_core_session_t *session = switch_core_media_bug_get_session(bug);
switch_codec_implementation_t read_impl = { 0 };
switch_core_session_get_read_impl(session, &read_impl);
samd_set_sample_rate(amd, read_impl.actual_samples_per_second);
break;
}
case SWITCH_ABC_TYPE_READ_REPLACE: {
switch_frame_t *frame;
if ((frame = switch_core_media_bug_get_read_replace_frame(bug))) {
samd_process_buffer(amd, frame->data, frame->samples, frame->channels);
switch_core_media_bug_set_read_replace_frame(bug, frame);
}
break;
}
case SWITCH_ABC_TYPE_CLOSE:
samd_destroy(&amd);
break;
default:
break;
}
return SWITCH_TRUE;
}
static samd_t *create_amd(switch_core_session_t *session, const char *args)
{
samd_t *amd = NULL;
/* create detector */
samd_init(&amd);
samd_set_log_handler(amd, log_handler, session);
samd_set_event_handler(amd, amd_event_handler, session);
/* configure detector */
if (!zstr(args)) {
char *largs = switch_core_session_strdup(session, args);
switch_event_t *params = NULL;
if (switch_event_create_brackets(largs, '{', '}', ',', ¶ms, &largs, SWITCH_FALSE) == SWITCH_STATUS_SUCCESS) {
const char *val;
configure_vad(samd_get_vad(amd), session, params);
if ((val = switch_event_get_header(params, "wait_for_voice_ms"))) {
int v;
if (switch_is_number(val) && (v = atoi(val)) > 0) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "wait_for_voice_ms = %d\n", v);
samd_set_wait_for_voice_ms(amd, v);
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Ignoring invalid wait_for_voice_ms = \"%s\"\n", val);
}
}
if ((val = switch_event_get_header(params, "machine_ms"))) {
int v;
if (switch_is_number(val) && (v = atoi(val)) > 0) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "machine_ms = %d\n", v);
samd_set_machine_ms(amd, v);
} else {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Ignoring invalid machine_ms = \"%s\"\n", val);
}
}
}
if (params) {
switch_event_destroy(¶ms);
}
}
return amd;
}
/**
* APP interface to start AMD
*/
SWITCH_STANDARD_APP(simpleamd_start_app)
{
switch_channel_t *channel = switch_core_session_get_channel(session);
switch_media_bug_t *bug = NULL;
samd_t *amd = NULL;
/* are we already running? */
bug = switch_channel_get_private(channel, "_mod_simpleamd_amd");
if (bug) {
switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "-ERR already running");
return;
}
amd = create_amd(session, data);
/* Add media bug */
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Starting AMD\n");
switch_core_media_bug_add(session, "_mod_simpleamd_amd", NULL, amd_process_buffer, amd, 0, SMBF_NO_PAUSE | SMBF_READ_REPLACE, &bug);
if (!bug) {
samd_destroy(&amd);
switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "-ERR can't create media bug");
return;
}
switch_channel_set_private(channel, "_mod_simpleamd_amd", bug);
switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "+OK started");
}
/**
* APP interface to stop AMD
*/
SWITCH_STANDARD_APP(simpleamd_stop_app)
{
switch_channel_t *channel = switch_core_session_get_channel(session);
switch_media_bug_t *bug = switch_channel_get_private(channel, "_mod_simpleamd_amd");
if (bug) {
switch_core_media_bug_remove(session, &bug);
switch_channel_set_private(channel, "_mod_simpleamd_amd", NULL);
switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "+OK stopped");
} else {
switch_channel_set_variable(channel, SWITCH_CURRENT_APPLICATION_RESPONSE_VARIABLE, "-ERR not running");
}
}
/**
* Called when FreeSWITCH loads the module
*/
SWITCH_MODULE_LOAD_FUNCTION(mod_simpleamd_load)
{
switch_application_interface_t *app;
*module_interface = switch_loadable_module_create_module_interface(pool, modname);
SWITCH_ADD_APP(app, "simplevad_start", "Start VAD", "Start VAD", simplevad_start_app, SIMPLEVAD_START_SYNTAX, SAF_MEDIA_TAP);
SWITCH_ADD_APP(app, "simplevad_stop", "Stop VAD", "Stop VAD", simplevad_stop_app, SIMPLEVAD_STOP_SYNTAX, SAF_NONE);
SWITCH_ADD_APP(app, "simpleamd_start", "Start AMD", "Start AMD w/ beep detection", simpleamd_start_app, SIMPLEAMD_START_SYNTAX, SAF_MEDIA_TAP);
SWITCH_ADD_APP(app, "simpleamd_stop", "Stop AMD", "Stop AMD w/ beep detection", simpleamd_stop_app, SIMPLEAMD_STOP_SYNTAX, SAF_NONE);
#if 0
SWITCH_ADD_APP(app, "simplebeep_start", "Start beep detector only", "Start beep detector", simplebeep_start_app, SIMPLEBEEP_START_SYNTAX, SAF_MEDIA_TAP);
SWITCH_ADD_APP(app, "simplebeep_stop", "Stop beep detector", "Stop beep detector", simplebeep_stop_app, SIMPLEBEEP_STOP_SYNTAX, SAF_NONE);
#endif
/* indicate that the module should continue to be loaded */
return SWITCH_STATUS_SUCCESS;
}
/**
* Called when FreeSWITCH stops the module
*/
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_simpleamd_shutdown)
{
return SWITCH_STATUS_SUCCESS;
}
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
*/