123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- #include "asterisk.h"
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include "asterisk/module.h"
- #include "asterisk/channel.h"
- #include "asterisk/pbx.h"
- #include "asterisk/app.h"
- #include "asterisk/dsp.h"
- #include "asterisk/audiohook.h"
- #include "asterisk/stasis.h"
- #include "asterisk/stasis_channels.h"
- #define DEFAULT_SILENCE_THRESHOLD 2500
- struct talk_detect_params {
-
- struct ast_audiohook audiohook;
-
- int dsp_talking_threshold;
-
- int dsp_silence_threshold;
-
- int talking;
-
- struct timeval talking_start;
-
- struct ast_dsp *dsp;
- };
- static void datastore_destroy_cb(void *data) {
- struct talk_detect_params *td_params = data;
- ast_audiohook_destroy(&td_params->audiohook);
- if (td_params->dsp) {
- ast_dsp_free(td_params->dsp);
- }
- ast_free(data);
- }
- static const struct ast_datastore_info talk_detect_datastore = {
- .type = "talk_detect",
- .destroy = datastore_destroy_cb
- };
- static int talk_detect_audiohook_cb(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
- {
- int total_silence;
- int update_talking = 0;
- struct ast_datastore *datastore;
- struct talk_detect_params *td_params;
- struct stasis_message *message;
- if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
- return 1;
- }
- if (direction != AST_AUDIOHOOK_DIRECTION_READ) {
- return 1;
- }
- if (frame->frametype != AST_FRAME_VOICE) {
- return 1;
- }
- if (!(datastore = ast_channel_datastore_find(chan, &talk_detect_datastore, NULL))) {
- return 1;
- }
- td_params = datastore->data;
- ast_dsp_silence(td_params->dsp, frame, &total_silence);
- if (total_silence < td_params->dsp_silence_threshold) {
- if (!td_params->talking) {
- update_talking = 1;
- td_params->talking_start = ast_tvnow();
- }
- td_params->talking = 1;
- } else {
- if (td_params->talking) {
- update_talking = 1;
- }
- td_params->talking = 0;
- }
- if (update_talking) {
- struct ast_json *blob = NULL;
- if (!td_params->talking) {
- int64_t diff_ms = ast_tvdiff_ms(ast_tvnow(), td_params->talking_start);
- diff_ms -= td_params->dsp_silence_threshold;
- blob = ast_json_pack("{s: i}", "duration", diff_ms);
- if (!blob) {
- return 1;
- }
- }
- ast_verb(4, "%s is now %s\n", ast_channel_name(chan),
- td_params->talking ? "talking" : "silent");
- message = ast_channel_blob_create_from_cache(ast_channel_uniqueid(chan),
- td_params->talking ? ast_channel_talking_start() : ast_channel_talking_stop(),
- blob);
- if (message) {
- stasis_publish(ast_channel_topic(chan), message);
- ao2_ref(message, -1);
- }
- ast_json_unref(blob);
- }
- return 1;
- }
- static int remove_talk_detect(struct ast_channel *chan)
- {
- struct ast_datastore *datastore = NULL;
- struct talk_detect_params *td_params;
- SCOPED_CHANNELLOCK(chan_lock, chan);
- datastore = ast_channel_datastore_find(chan, &talk_detect_datastore, NULL);
- if (!datastore) {
- ast_log(AST_LOG_WARNING, "Cannot remove TALK_DETECT from %s: TALK_DETECT not currently enabled\n",
- ast_channel_name(chan));
- return -1;
- }
- td_params = datastore->data;
- if (ast_audiohook_remove(chan, &td_params->audiohook)) {
- ast_log(AST_LOG_WARNING, "Failed to remove TALK_DETECT audiohook from channel %s\n",
- ast_channel_name(chan));
- return -1;
- }
- if (ast_channel_datastore_remove(chan, datastore)) {
- ast_log(AST_LOG_WARNING, "Failed to remove TALK_DETECT datastore from channel %s\n",
- ast_channel_name(chan));
- return -1;
- }
- ast_datastore_free(datastore);
- return 0;
- }
- static int set_talk_detect(struct ast_channel *chan, int dsp_silence_threshold, int dsp_talking_threshold)
- {
- struct ast_datastore *datastore = NULL;
- struct talk_detect_params *td_params;
- SCOPED_CHANNELLOCK(chan_lock, chan);
- datastore = ast_channel_datastore_find(chan, &talk_detect_datastore, NULL);
- if (!datastore) {
- datastore = ast_datastore_alloc(&talk_detect_datastore, NULL);
- if (!datastore) {
- return -1;
- }
- td_params = ast_calloc(1, sizeof(*td_params));
- if (!td_params) {
- ast_datastore_free(datastore);
- return -1;
- }
- ast_audiohook_init(&td_params->audiohook,
- AST_AUDIOHOOK_TYPE_MANIPULATE,
- "TALK_DETECT",
- AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
- td_params->audiohook.manipulate_callback = talk_detect_audiohook_cb;
- ast_set_flag(&td_params->audiohook, AST_AUDIOHOOK_TRIGGER_READ);
- td_params->dsp = ast_dsp_new_with_rate(ast_format_rate(ast_channel_rawreadformat(chan)));
- if (!td_params->dsp) {
- ast_datastore_free(datastore);
- ast_free(td_params);
- return -1;
- }
- datastore->data = td_params;
- ast_channel_datastore_add(chan, datastore);
- ast_audiohook_attach(chan, &td_params->audiohook);
- } else {
-
- td_params = datastore->data;
- }
- td_params->dsp_talking_threshold = dsp_talking_threshold;
- td_params->dsp_silence_threshold = dsp_silence_threshold;
- ast_dsp_set_threshold(td_params->dsp, td_params->dsp_talking_threshold);
- return 0;
- }
- static int talk_detect_fn_write(struct ast_channel *chan, const char *function, char *data, const char *value)
- {
- int res;
- if (!chan) {
- return -1;
- }
- if (ast_strlen_zero(data)) {
- ast_log(AST_LOG_WARNING, "TALK_DETECT requires an argument\n");
- return -1;
- }
- if (!strcasecmp(data, "set")) {
- int dsp_silence_threshold = DEFAULT_SILENCE_THRESHOLD;
- int dsp_talking_threshold = ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE);
- if (!ast_strlen_zero(value)) {
- char *parse = ast_strdupa(value);
- AST_DECLARE_APP_ARGS(args,
- AST_APP_ARG(silence_threshold);
- AST_APP_ARG(talking_threshold);
- );
- AST_STANDARD_APP_ARGS(args, parse);
- if (!ast_strlen_zero(args.silence_threshold)) {
- if (sscanf(args.silence_threshold, "%30d", &dsp_silence_threshold) != 1) {
- ast_log(AST_LOG_WARNING, "Failed to parse %s for dsp_silence_threshold\n",
- args.silence_threshold);
- return -1;
- }
- if (dsp_silence_threshold < 1) {
- ast_log(AST_LOG_WARNING, "Invalid value %d for dsp_silence_threshold\n",
- dsp_silence_threshold);
- return -1;
- }
- }
- if (!ast_strlen_zero(args.talking_threshold)) {
- if (sscanf(args.talking_threshold, "%30d", &dsp_talking_threshold) != 1) {
- ast_log(AST_LOG_WARNING, "Failed to parse %s for dsp_talking_threshold\n",
- args.talking_threshold);
- return -1;
- }
- if (dsp_talking_threshold < 1) {
- ast_log(AST_LOG_WARNING, "Invalid value %d for dsp_talking_threshold\n",
- dsp_silence_threshold);
- return -1;
- }
- }
- }
- res = set_talk_detect(chan, dsp_silence_threshold, dsp_talking_threshold);
- } else if (!strcasecmp(data, "remove")) {
- res = remove_talk_detect(chan);
- } else {
- ast_log(AST_LOG_WARNING, "TALK_DETECT: unknown option %s\n", data);
- res = -1;
- }
- return res;
- }
- static struct ast_custom_function talk_detect_function = {
- .name = "TALK_DETECT",
- .write = talk_detect_fn_write,
- };
- static int unload_module(void)
- {
- int res = 0;
- res |= ast_custom_function_unregister(&talk_detect_function);
- return res;
- }
- static int load_module(void)
- {
- int res = 0;
- res |= ast_custom_function_register(&talk_detect_function);
- return res ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SUCCESS;
- }
- AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Talk detection dialplan function");
|