41 #include "asterisk/stasis_channels.h"
136 #define DEFAULT_SILENCE_THRESHOLD 2500
155 static void datastore_destroy_cb(
void *data) {
160 if (td_params->
dsp) {
161 ast_dsp_free(td_params->
dsp);
168 .
type =
"talk_detect",
169 .destroy = datastore_destroy_cb
184 int update_talking = 0;
204 td_params = datastore->
data;
220 if (update_talking) {
233 ast_verb(4,
"%s is now %s\n", ast_channel_name(chan),
234 td_params->
talking ?
"talking" :
"silent");
250 static int remove_talk_detect(
struct ast_channel *chan)
258 ast_log(AST_LOG_WARNING,
"Cannot remove TALK_DETECT from %s: TALK_DETECT not currently enabled\n",
259 ast_channel_name(chan));
262 td_params = datastore->
data;
265 ast_log(AST_LOG_WARNING,
"Failed to remove TALK_DETECT audiohook from channel %s\n",
266 ast_channel_name(chan));
271 ast_log(AST_LOG_WARNING,
"Failed to remove TALK_DETECT datastore from channel %s\n",
272 ast_channel_name(chan));
289 datastore = ast_datastore_alloc(&talk_detect_datastore, NULL);
294 td_params =
ast_calloc(1,
sizeof(*td_params));
308 if (!td_params->
dsp) {
313 datastore->
data = td_params;
319 td_params = datastore->
data;
331 static int talk_detect_fn_write(
struct ast_channel *chan,
const char *
function,
char *data,
const char *value)
339 if (ast_strlen_zero(data)) {
340 ast_log(AST_LOG_WARNING,
"TALK_DETECT requires an argument\n");
344 if (!strcasecmp(data,
"set")) {
348 if (!ast_strlen_zero(value)) {
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);
365 if (dsp_silence_threshold < 1) {
366 ast_log(AST_LOG_WARNING,
"Invalid value %d for dsp_silence_threshold\n",
367 dsp_silence_threshold);
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);
379 if (dsp_talking_threshold < 1) {
380 ast_log(AST_LOG_WARNING,
"Invalid value %d for dsp_talking_threshold\n",
381 dsp_talking_threshold);
387 res = set_talk_detect(chan, dsp_silence_threshold, dsp_talking_threshold);
388 }
else if (!strcasecmp(data,
"remove")) {
389 res = remove_talk_detect(chan);
391 ast_log(AST_LOG_WARNING,
"TALK_DETECT: unknown option %s\n", data);
400 .
name =
"TALK_DETECT",
401 .write = talk_detect_fn_write,
405 static int unload_module(
void)
415 static int load_module(
void)
424 AST_MODULE_INFO_STANDARD(
ASTERISK_GPL_KEY,
"Talk detection dialplan function");
struct stasis_message_type * ast_channel_talking_stop(void)
Message type for a channel stopping talking.
Main Channel structure associated with a channel.
Asterisk main include file. File version handling, generic pbx functions.
struct ast_json * ast_json_pack(char const *format,...)
Helper for creating complex JSON values.
int dsp_talking_threshold
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Convenient Signal Processing routines.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
Stasis Message Bus API. See Stasis Message Bus API for detailed documentation.
static const int DEFAULT_SILENCE_THRESHOLD
The default silence threshold we will use if an alternate configured value is not present or is inval...
AST_JSON_INT_T ast_json_int_t
Primarily used to cast when packing to an "I" type.
int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook)
Remove an audiohook from a specified channel.
Structure for a data store type.
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
Attach audiohook to channel.
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Structure for a data store object.
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
struct stasis_message_type * ast_channel_talking_start(void)
Message type for a channel starting talking.
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
ast_audiohook_manipulate_callback manipulate_callback
int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source, enum ast_audiohook_init_flags flags)
Initialize an audiohook structure.
#define SCOPED_CHANNELLOCK(varname, chan)
scoped lock specialization for channels.
General Asterisk PBX channel definitions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Data structure associated with a custom dialplan function.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
int dsp_silence_threshold
Core PBX routines and definitions.
Private data structure used with the function's datastore.
void ast_dsp_set_threshold(struct ast_dsp *dsp, int threshold)
Set the minimum average magnitude threshold to determine talking by the DSP.
struct stasis_topic * ast_channel_topic(struct ast_channel *chan)
A topic which publishes the events for a particular channel.
struct ast_audiohook audiohook
static const struct ast_datastore_info talk_detect_datastore
The channel datastore the function uses to store state.
void stasis_publish(struct stasis_topic *topic, struct stasis_message *message)
Publish a message to a topic's subscribers.
static struct ast_custom_function talk_detect_function
Definition of the TALK_DETECT function.
#define ast_calloc(num, len)
A wrapper for calloc()
Module has failed to load, may be in an inconsistent state.
struct stasis_message * ast_channel_blob_create_from_cache(const char *uniqueid, struct stasis_message_type *type, struct ast_json *blob)
Create a ast_channel_blob message, pulling channel state from the cache.
int ast_dsp_silence(struct ast_dsp *dsp, struct ast_frame *f, int *totalsilence)
Process the audio frame for silence.
Data structure associated with a single frame of data.
Abstract JSON element (object, array, string, int, ...).
struct ast_dsp * ast_dsp_new_with_rate(unsigned int sample_rate)
Allocates a new dsp with a specific internal sample rate used during processing.
enum ast_audiohook_status status
enum ast_frame_type frametype
#define ASTERISK_GPL_KEY
The text the key() function should return.
int ast_dsp_get_threshold_from_settings(enum threshold which)
Get silence threshold from dsp.conf.
Asterisk module definitions.
struct timeval talking_start
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
Remove a datastore from a channel.
#define ast_custom_function_register(acf)
Register a custom function.
#define AST_APP_ARG(name)
Define an application argument.