| 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 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include "asterisk/module.h" |
| 35 |
#include "asterisk/channel.h" |
| 36 |
#include "asterisk/pbx.h" |
| 37 |
#include "asterisk/app.h" |
| 38 |
#include "asterisk/dsp.h" |
| 39 |
#include "asterisk/audiohook.h" |
| 40 |
#include "asterisk/stasis.h" |
| 41 |
#include "asterisk/stasis_channels.h" |
| 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 |
#define DEFAULT_SILENCE_THRESHOLD 2500 |
| 137 |
|
| 138 |
|
| 139 |
struct talk_detect_params { |
| 140 |
|
| 141 |
struct ast_audiohook audiohook; |
| 142 |
|
| 143 |
int dsp_talking_threshold; |
| 144 |
|
| 145 |
int dsp_silence_threshold; |
| 146 |
|
| 147 |
int talking; |
| 148 |
|
| 149 |
struct timeval talking_start; |
| 150 |
|
| 151 |
struct ast_dsp *dsp; |
| 152 |
}; |
| 153 |
|
| 154 |
|
| 155 |
static void datastore_destroy_cb(void *data) { |
| 156 |
struct talk_detect_params *td_params = data; |
| 157 |
|
| 158 |
ast_audiohook_destroy(&td_params->audiohook); |
| 159 |
|
| 160 |
if (td_params->dsp) { |
| 161 |
ast_dsp_free(td_params->dsp); |
| 162 |
} |
| 163 |
ast_free(data); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
static const struct ast_datastore_info talk_detect_datastore = { |
| 168 |
.type = "talk_detect", |
| 169 |
.destroy = datastore_destroy_cb |
| 170 |
}; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
static int talk_detect_audiohook_cb(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction) |
| 181 |
{ |
| 182 |
int total_silence; |
| 183 |
int is_talking; |
| 184 |
int update_talking = 0; |
| 185 |
struct ast_datastore *datastore; |
| 186 |
struct talk_detect_params *td_params; |
| 187 |
struct stasis_message *message; |
| 188 |
|
| 189 |
if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) { |
| 190 |
return 1; |
| 191 |
} |
| 192 |
|
| 193 |
if (direction != AST_AUDIOHOOK_DIRECTION_READ) { |
| 194 |
return 1; |
| 195 |
} |
| 196 |
|
| 197 |
if (frame->frametype != AST_FRAME_VOICE) { |
| 198 |
return 1; |
| 199 |
} |
| 200 |
|
| 201 |
if (!(datastore = ast_channel_datastore_find(chan, &talk_detect_datastore, NULL))) { |
| 202 |
return 1; |
| 203 |
} |
| 204 |
td_params = datastore->data; |
| 205 |
|
| 206 |
is_talking = !ast_dsp_silence(td_params->dsp, frame, &total_silence); |
| 207 |
if (is_talking) { |
| 208 |
if (!td_params->talking) { |
| 209 |
update_talking = 1; |
| 210 |
td_params->talking_start = ast_tvnow(); |
| 211 |
} |
| 212 |
td_params->talking = 1; |
| 213 |
} else if (total_silence >= td_params->dsp_silence_threshold) { |
| 214 |
if (td_params->talking) { |
| 215 |
update_talking = 1; |
| 216 |
} |
| 217 |
td_params->talking = 0; |
| 218 |
} |
| 219 |
|
| 220 |
if (update_talking) { |
| 221 |
struct ast_json *blob = NULL; |
| 222 |
|
| 223 |
if (!td_params->talking) { |
| 224 |
int64_t diff_ms = ast_tvdiff_ms(ast_tvnow(), td_params->talking_start); |
| 225 |
diff_ms -= td_params->dsp_silence_threshold; |
| 226 |
|
| 227 |
blob = ast_json_pack("{s: I}", "duration", (ast_json_int_t)diff_ms); |
| 228 |
if (!blob) { |
| 229 |
return 1; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
ast_verb(4, "%s is now %s\n", ast_channel_name(chan), |
| 234 |
td_params->talking ? "talking" : "silent"); |
| 235 |
message = ast_channel_blob_create_from_cache(ast_channel_uniqueid(chan), |
| 236 |
td_params->talking ? ast_channel_talking_start() : ast_channel_talking_stop(), |
| 237 |
blob); |
| 238 |
if (message) { |
| 239 |
stasis_publish(ast_channel_topic(chan), message); |
| 240 |
ao2_ref(message, -1); |
| 241 |
} |
| 242 |
|
| 243 |
ast_json_unref(blob); |
| 244 |
} |
| 245 |
|
| 246 |
return 1; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
static int remove_talk_detect(struct ast_channel *chan) |
| 251 |
{ |
| 252 |
struct ast_datastore *datastore = NULL; |
| 253 |
struct talk_detect_params *td_params; |
| 254 |
SCOPED_CHANNELLOCK(chan_lock, chan); |
| 255 |
|
| 256 |
datastore = ast_channel_datastore_find(chan, &talk_detect_datastore, NULL); |
| 257 |
if (!datastore) { |
| 258 |
ast_log(AST_LOG_WARNING, "Cannot remove TALK_DETECT from %s: TALK_DETECT not currently enabled\n", |
| 259 |
ast_channel_name(chan)); |
| 260 |
return -1; |
| 261 |
} |
| 262 |
td_params = datastore->data; |
| 263 |
|
| 264 |
if (ast_audiohook_remove(chan, &td_params->audiohook)) { |
| 265 |
ast_log(AST_LOG_WARNING, "Failed to remove TALK_DETECT audiohook from channel %s\n", |
| 266 |
ast_channel_name(chan)); |
| 267 |
return -1; |
| 268 |
} |
| 269 |
|
| 270 |
if (ast_channel_datastore_remove(chan, datastore)) { |
| 271 |
ast_log(AST_LOG_WARNING, "Failed to remove TALK_DETECT datastore from channel %s\n", |
| 272 |
ast_channel_name(chan)); |
| 273 |
return -1; |
| 274 |
} |
| 275 |
ast_datastore_free(datastore); |
| 276 |
|
| 277 |
return 0; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
static int set_talk_detect(struct ast_channel *chan, int dsp_silence_threshold, int dsp_talking_threshold) |
| 282 |
{ |
| 283 |
struct ast_datastore *datastore = NULL; |
| 284 |
struct talk_detect_params *td_params; |
| 285 |
SCOPED_CHANNELLOCK(chan_lock, chan); |
| 286 |
|
| 287 |
datastore = ast_channel_datastore_find(chan, &talk_detect_datastore, NULL); |
| 288 |
if (!datastore) { |
| 289 |
datastore = ast_datastore_alloc(&talk_detect_datastore, NULL); |
| 290 |
if (!datastore) { |
| 291 |
return -1; |
| 292 |
} |
| 293 |
|
| 294 |
td_params = ast_calloc(1, sizeof(*td_params)); |
| 295 |
if (!td_params) { |
| 296 |
ast_datastore_free(datastore); |
| 297 |
return -1; |
| 298 |
} |
| 299 |
|
| 300 |
ast_audiohook_init(&td_params->audiohook, |
| 301 |
AST_AUDIOHOOK_TYPE_MANIPULATE, |
| 302 |
"TALK_DETECT", |
| 303 |
AST_AUDIOHOOK_MANIPULATE_ALL_RATES); |
| 304 |
td_params->audiohook.manipulate_callback = talk_detect_audiohook_cb; |
| 305 |
ast_set_flag(&td_params->audiohook, AST_AUDIOHOOK_TRIGGER_READ); |
| 306 |
|
| 307 |
td_params->dsp = ast_dsp_new_with_rate(ast_format_get_sample_rate(ast_channel_rawreadformat(chan))); |
| 308 |
if (!td_params->dsp) { |
| 309 |
ast_datastore_free(datastore); |
| 310 |
ast_free(td_params); |
| 311 |
return -1; |
| 312 |
} |
| 313 |
datastore->data = td_params; |
| 314 |
|
| 315 |
ast_channel_datastore_add(chan, datastore); |
| 316 |
ast_audiohook_attach(chan, &td_params->audiohook); |
| 317 |
} else { |
| 318 |
|
| 319 |
td_params = datastore->data; |
| 320 |
} |
| 321 |
|
| 322 |
td_params->dsp_talking_threshold = dsp_talking_threshold; |
| 323 |
td_params->dsp_silence_threshold = dsp_silence_threshold; |
| 324 |
|
| 325 |
ast_dsp_set_threshold(td_params->dsp, td_params->dsp_talking_threshold); |
| 326 |
|
| 327 |
return 0; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
static int talk_detect_fn_write(struct ast_channel *chan, const char *function, char *data, const char *value) |
| 332 |
{ |
| 333 |
int res; |
| 334 |
|
| 335 |
if (!chan) { |
| 336 |
return -1; |
| 337 |
} |
| 338 |
|
| 339 |
if (ast_strlen_zero(data)) { |
| 340 |
ast_log(AST_LOG_WARNING, "TALK_DETECT requires an argument\n"); |
| 341 |
return -1; |
| 342 |
} |
| 343 |
|
| 344 |
if (!strcasecmp(data, "set")) { |
| 345 |
int dsp_silence_threshold = DEFAULT_SILENCE_THRESHOLD; |
| 346 |
int dsp_talking_threshold = ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE); |
| 347 |
|
| 348 |
if (!ast_strlen_zero(value)) { |
| 349 |
char *parse = ast_strdupa(value); |
| 350 |
|
| 351 |
AST_DECLARE_APP_ARGS(args, |
| 352 |
AST_APP_ARG(silence_threshold); |
| 353 |
AST_APP_ARG(talking_threshold); |
| 354 |
); |
| 355 |
|
| 356 |
AST_STANDARD_APP_ARGS(args, parse); |
| 357 |
|
| 358 |
if (!ast_strlen_zero(args.silence_threshold)) { |
| 359 |
if (sscanf(args.silence_threshold, "%30d", &dsp_silence_threshold) != 1) { |
| 360 |
ast_log(AST_LOG_WARNING, "Failed to parse %s for dsp_silence_threshold\n", |
| 361 |
args.silence_threshold); |
| 362 |
return -1; |
| 363 |
} |
| 364 |
|
| 365 |
if (dsp_silence_threshold < 1) { |
| 366 |
ast_log(AST_LOG_WARNING, "Invalid value %d for dsp_silence_threshold\n", |
| 367 |
dsp_silence_threshold); |
| 368 |
return -1; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
if (!ast_strlen_zero(args.talking_threshold)) { |
| 373 |
if (sscanf(args.talking_threshold, "%30d", &dsp_talking_threshold) != 1) { |
| 374 |
ast_log(AST_LOG_WARNING, "Failed to parse %s for dsp_talking_threshold\n", |
| 375 |
args.talking_threshold); |
| 376 |
return -1; |
| 377 |
} |
| 378 |
|
| 379 |
if (dsp_talking_threshold < 1) { |
| 380 |
ast_log(AST_LOG_WARNING, "Invalid value %d for dsp_talking_threshold\n", |
| 381 |
dsp_talking_threshold); |
| 382 |
return -1; |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
res = set_talk_detect(chan, dsp_silence_threshold, dsp_talking_threshold); |
| 388 |
} else if (!strcasecmp(data, "remove")) { |
| 389 |
res = remove_talk_detect(chan); |
| 390 |
} else { |
| 391 |
ast_log(AST_LOG_WARNING, "TALK_DETECT: unknown option %s\n", data); |
| 392 |
res = -1; |
| 393 |
} |
| 394 |
|
| 395 |
return res; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
static struct ast_custom_function talk_detect_function = { |
| 400 |
.name = "TALK_DETECT", |
| 401 |
.write = talk_detect_fn_write, |
| 402 |
}; |
| 403 |
|
| 404 |
|
| 405 |
static int unload_module(void) |
| 406 |
{ |
| 407 |
int res = 0; |
| 408 |
|
| 409 |
res |= ast_custom_function_unregister(&talk_detect_function); |
| 410 |
|
| 411 |
return res; |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
static int load_module(void) |
| 416 |
{ |
| 417 |
int res = 0; |
| 418 |
|
| 419 |
res |= ast_custom_function_register(&talk_detect_function); |
| 420 |
|
| 421 |
return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS; |
| 422 |
} |
| 423 |
|
| 424 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Talk detection dialplan function"); |