Changing syntax once again slightly and standardizing
[asterisk/asterisk.git] / apps / app_amd.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2003 - 2006, Aheeva Technology.
5  *
6  * Claude Klimos (claude.klimos@aheeva.com)
7  *
8  * Disclaimed to Digium
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "asterisk/module.h"
25 #include "asterisk/lock.h"
26 #include "asterisk/options.h"
27 #include "asterisk/channel.h"
28 #include "asterisk/dsp.h"
29 #include "asterisk/pbx.h"
30 #include "asterisk/config.h"
31 #include "asterisk/app.h"
32
33
34 static char *tdesc = "Answering Machine Detection Application";
35 static char *app = "AMD";
36 static char *synopsis = "Attempts to detect answering machines";
37 static char *descrip =
38 "  AMD([initialSilence][|greeting][|afterGreetingSilence][|totalAnalysisTime]\n"
39 "      [|minimumWordLength][|betweenWordsSilence][|maximumNumberOfWords]\n"
40 "      [|silenceThreshold])\n"
41 "  This application attempts to detect answering machines at the beginning\n"
42 "  of outbound calls.  Simply call this application after the call\n"
43 "  has been answered (outbound only, of course).\n"
44 "  When loaded, AMD reads amd.conf and uses the parameters specified as\n"
45 "  default values. Those default values get overwritten when calling AMD\n"
46 "  with parameters.\n"
47 "- 'initialSilence' is the maximum silence duration before the greeting. If\n"
48 "   exceeded then MACHINE.\n"
49 "- 'greeting' is the maximum length of a greeting. If exceeded then MACHINE.\n"
50 "- 'afterGreetingSilence' is the silence after detecting a greeting.\n"
51 "   If exceeded then HUMAN.\n"
52 "- 'totalAnalysisTime' is the maximum time allowed for the algorithm to decide\n"
53 "   on a HUMAN or MACHINE.\n"
54 "- 'minimumWordLength'is the minimum duration of Voice to considered as a word.\n"
55 "- 'betweenWordsSilence' is the minimum duration of silence after a word to \n"
56 "   consider the audio that follows as a new word.\n"
57 "- 'maximumNumberOfWords'is the maximum number of words in the greeting. \n"
58 "   If exceeded then MACHINE.\n"
59 "- 'silenceThreshold' is the silence threshold.\n"
60 "This application sets the following channel variable upon completion:\n"
61 "    AMDSTATUS - This is the status of the answering machine detection.\n"
62 "                Possible values are:\n"
63 "                MACHINE | HUMAN | NOTSURE | HANGUP\n"
64 "    AMDCAUSE - Indicates the cause that led to the conclusion.\n"
65 "               Possible values are:\n"
66 "               TOOLONG-<%d total_time>\n"
67 "               INITIALSILENCE-<%d silenceDuration>-<%d initialSilence>\n"
68 "               HUMAN-<%d silenceDuration>-<%d afterGreetingSilence>\n"
69 "               MAXWORDS-<%d wordsCount>-<%d maximumNumberOfWords>\n"
70 "               LONGGREETING-<%d voiceDuration>-<%d greeting>\n";
71
72
73 LOCAL_USER_DECL;
74
75 #define STATE_IN_WORD       1
76 #define STATE_IN_SILENCE    2
77
78 /* Some default values for the algorithm parameters. These defaults will be overwritten from amd.conf */
79 static int dfltInitialSilence       = 2500;
80 static int dfltGreeting             = 1500;
81 static int dfltAfterGreetingSilence = 800;
82 static int dfltTotalAnalysisTime    = 5000;
83 static int dfltMinimumWordLength    = 100;
84 static int dfltBetweenWordsSilence  = 50;
85 static int dfltMaximumNumberOfWords = 3;
86 static int dfltSilenceThreshold     = 256;
87
88 static void isAnsweringMachine(struct ast_channel *chan, void *data)
89 {
90         int res = 0;
91
92         struct ast_frame *f = NULL;
93
94         struct ast_dsp *silenceDetector;         /* silence detector dsp */
95         int dspsilence = 0;
96         int readFormat;
97         int framelength;
98
99         int inInitialSilence         = 1;
100         int inGreeting               = 0;
101         int voiceDuration            = 0;
102         int silenceDuration          = 0;
103         int iTotalTime               = 0;
104         int iWordsCount              = 0;
105         int currentState             = STATE_IN_SILENCE;
106         int previousState            = STATE_IN_SILENCE;
107         int consecutiveVoiceDuration = 0;
108         char amdCause[256]           = "";
109         char amdStatus[256]          = "";
110
111         /* Lets set the initial values of the variables that will control the algorithm.
112            The initial values are the default ones. If they are passed as arguments
113            when invoking the application, then the default values will be overwritten
114            by the ones passed as parameters. */
115         int initialSilence       = dfltInitialSilence;
116         int greeting             = dfltGreeting;
117         int afterGreetingSilence = dfltAfterGreetingSilence;
118         int totalAnalysisTime    = dfltTotalAnalysisTime;
119         int minimumWordLength    = dfltMinimumWordLength;
120         int betweenWordsSilence  = dfltBetweenWordsSilence;
121         int maximumNumberOfWords = dfltMaximumNumberOfWords;
122         int silenceThreshold     = dfltSilenceThreshold;
123
124         char *parse;
125         AST_DECLARE_APP_ARGS(args,
126                              AST_APP_ARG(argInitialSilence);
127                              AST_APP_ARG(argGreeting);
128                              AST_APP_ARG(argAfterGreetingSilence);
129                              AST_APP_ARG(argTotalAnalysisTime);
130                              AST_APP_ARG(argMinimumWordLength);
131                              AST_APP_ARG(argBetweenWordsSilence);
132                              AST_APP_ARG(argMaximumNumberOfWords);
133                              AST_APP_ARG(argSilenceThreshold);
134         );
135
136         ast_verbose(VERBOSE_PREFIX_3 "AMD: %s %s %s (Fmt: %d)\n", chan->name ,chan->cid.cid_ani, chan->cid.cid_rdnis, chan->readformat);
137
138         /* Lets parse the arguments. */
139         if (ast_strlen_zero(data)) {
140                 ast_log(LOG_NOTICE, "AMD using the default parameters.\n");
141         } else {
142                 /* Some arguments have been passed. Lets parse them and overwrite the defaults. */
143                 if (!(parse = ast_strdupa(data))) {
144                         ast_log(LOG_WARNING, "Memory allocation failure\n");
145                         pbx_builtin_setvar_helper(chan , "AMDSTATUS" , "" );
146                         pbx_builtin_setvar_helper(chan , "AMDCAUSE" , "" );
147                         return;
148                 }
149
150                 AST_STANDARD_APP_ARGS(args, parse);
151
152                 if (!ast_strlen_zero(args.argInitialSilence)) {
153                         initialSilence = atoi(args.argInitialSilence);
154                 }
155                 if (!ast_strlen_zero(args.argGreeting)) {
156                         greeting = atoi(args.argGreeting);
157                 }
158                 if (!ast_strlen_zero(args.argAfterGreetingSilence)) {
159                         afterGreetingSilence = atoi(args.argAfterGreetingSilence);
160                 }
161                 if (!ast_strlen_zero(args.argTotalAnalysisTime)) {
162                         totalAnalysisTime = atoi(args.argTotalAnalysisTime);
163                 }
164                 if (!ast_strlen_zero(args.argMinimumWordLength)) {
165                         minimumWordLength = atoi(args.argMinimumWordLength);
166                 }
167                 if (!ast_strlen_zero(args.argBetweenWordsSilence)) {
168                         betweenWordsSilence = atoi(args.argBetweenWordsSilence);
169                 }
170                 if (!ast_strlen_zero(args.argMaximumNumberOfWords)) {
171                         maximumNumberOfWords = atoi(args.argMaximumNumberOfWords);
172                 }
173                 if (!ast_strlen_zero(args.argSilenceThreshold)) {
174                         silenceThreshold = atoi(args.argSilenceThreshold);
175                 }
176         }
177
178         /* Now we're ready to roll! */
179
180         ast_verbose(VERBOSE_PREFIX_3 "AMD: initialSilence [%d] greeting [%d] afterGreetingSilence [%d] "
181                 "totalAnalysisTime [%d] minimumWordLength [%d] betweenWordsSilence [%d] maximumNumberOfWords [%d] silenceThreshold [%d] \n",
182                                 initialSilence, greeting, afterGreetingSilence, totalAnalysisTime,
183                                 minimumWordLength, betweenWordsSilence, maximumNumberOfWords, silenceThreshold );
184
185         readFormat = chan->readformat;
186         res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
187         if (res < 0 ) {
188                 ast_log(LOG_WARNING, "AMD: Channel [%s]. Unable to set to linear mode, giving up\n", chan->name );
189                 pbx_builtin_setvar_helper(chan , "AMDSTATUS" , "" );
190                 pbx_builtin_setvar_helper(chan , "AMDCAUSE" , "" );
191                 return;
192         }
193
194         silenceDetector = ast_dsp_new();
195         if (!silenceDetector ) {
196                 ast_log(LOG_WARNING, "AMD: Channel [%s]. Unable to create silence detector :(\n", chan->name );
197                 pbx_builtin_setvar_helper(chan , "AMDSTATUS" , "" );
198                 pbx_builtin_setvar_helper(chan , "AMDCAUSE" , "" );
199                 return;
200         }
201         ast_dsp_set_threshold(silenceDetector, silenceThreshold );
202
203         while (ast_waitfor(chan, -1) > -1)
204         {
205                 f = ast_read(chan);
206                 if (!f ) {
207                         /* No Frame: Called Party Must Have Dropped */
208                         ast_verbose(VERBOSE_PREFIX_3 "AMD: HANGUP\n");
209                         ast_log(LOG_DEBUG, "Got hangup\n");
210                         strcpy(amdStatus , "HANGUP" );
211                         strcpy(amdCause , "" );
212                         break;
213                 }
214                 framelength = (ast_codec_get_samples(f) / 8);
215                 iTotalTime += framelength;
216                 if (iTotalTime >= totalAnalysisTime ) {
217                         ast_verbose(VERBOSE_PREFIX_3 "AMD: Channel [%s]. Too long...\n", chan->name );
218                         ast_frfree(f);
219                         strcpy(amdStatus , "NOTSURE" );
220                         sprintf(amdCause , "TOOLONG-%d", iTotalTime );
221                         break;
222                 }
223                 if (f->frametype == AST_FRAME_VOICE ) {
224                         dspsilence = 0;
225                         ast_dsp_silence(silenceDetector, f, &dspsilence);
226                         if (dspsilence ) {
227                                 silenceDuration = dspsilence;
228                                 /* ast_verbose(VERBOSE_PREFIX_3 "AMD: %d SILENCE: silenceDuration:%d afterGreetingSilence:%d inGreeting:%d\n", currentState, silenceDuration, afterGreetingSilence, inGreeting ); */
229                                 if (silenceDuration >= betweenWordsSilence ) {
230                                         if (currentState != STATE_IN_SILENCE ) {
231                                                 previousState = currentState;
232                                                 ast_verbose(VERBOSE_PREFIX_3 "AMD: Changed state to STATE_IN_SILENCE\n");
233                                         }
234                                         currentState  = STATE_IN_SILENCE;
235                                         consecutiveVoiceDuration = 0;
236                                 }
237                                 if (inInitialSilence == 1  && silenceDuration >= initialSilence ) {
238                                         ast_verbose(VERBOSE_PREFIX_3 "AMD: ANSWERING MACHINE: silenceDuration:%d initialSilence:%d\n",
239                                                         silenceDuration, initialSilence );
240                                         ast_frfree(f);
241                                         strcpy(amdStatus , "MACHINE" );
242                                         sprintf(amdCause , "INITIALSILENCE-%d-%d", silenceDuration, initialSilence );
243                                         break;
244                                 }
245
246                                 if (silenceDuration >= afterGreetingSilence  &&  inGreeting == 1 ) {
247                                         ast_verbose(VERBOSE_PREFIX_3 "AMD: HUMAN: silenceDuration:%d afterGreetingSilence:%d\n",
248                                                         silenceDuration, afterGreetingSilence );
249                                         ast_frfree(f);
250                                         strcpy(amdStatus , "HUMAN" );
251                                         sprintf(amdCause , "HUMAN-%d-%d", silenceDuration, afterGreetingSilence );
252                                         break;
253                                 }
254                         } else {
255                                 consecutiveVoiceDuration += framelength;
256                                 voiceDuration += framelength;
257                                 /* ast_verbose(VERBOSE_PREFIX_3 "AMD: %d VOICE: ConsecutiveVoice:%d voiceDuration:%d inGreeting:%d\n", currentState, consecutiveVoiceDuration, voiceDuration, inGreeting ); */
258
259                                 /* If I have enough consecutive voice to say that I am in a Word, I can only increment the
260                                         number of words if my previous state was Silence, which means that I moved into a word. */
261                                 if (consecutiveVoiceDuration >= minimumWordLength ) {
262                                         if (currentState == STATE_IN_SILENCE ) {
263                                                 iWordsCount++;
264                                                 ast_verbose(VERBOSE_PREFIX_3 "AMD: Word detected. iWordsCount:%d\n", iWordsCount );
265                                                 previousState = currentState;
266                                                 currentState = STATE_IN_WORD;
267                                         }
268                                 }
269
270                                 if (iWordsCount >= maximumNumberOfWords ) {
271                                         ast_verbose(VERBOSE_PREFIX_3 "AMD: ANSWERING MACHINE: iWordsCount:%d\n", iWordsCount );
272                                         ast_frfree(f);
273                                         strcpy(amdStatus , "MACHINE" );
274                                         sprintf(amdCause , "MAXWORDS-%d-%d", iWordsCount, maximumNumberOfWords );
275                                         break;
276                                 }
277
278                                 if (inGreeting == 1  &&  voiceDuration >= greeting ) {
279                                         ast_verbose(VERBOSE_PREFIX_3 "AMD: ANSWERING MACHINE: voiceDuration:%d greeting:%d\n",
280                                                         voiceDuration, greeting );
281                                         ast_frfree(f);
282                                         strcpy(amdStatus , "MACHINE" );
283                                         sprintf(amdCause , "LONGGREETING-%d-%d", voiceDuration, greeting );
284                                         break;
285                                 }
286                                 if (voiceDuration >= minimumWordLength ) {
287                                         silenceDuration = 0;
288                                         inInitialSilence = 0;
289                                         inGreeting = 1;
290                                 }
291                         }
292                 }
293                 ast_frfree(f);
294         }
295
296         pbx_builtin_setvar_helper(chan , "AMDSTATUS" , amdStatus );
297         pbx_builtin_setvar_helper(chan , "AMDCAUSE" , amdCause );
298
299         /* If We Started With A Valid Read Format, Return To It... */
300         if (readFormat) {
301                 res = ast_set_read_format(chan, readFormat );
302                 if (res)
303                         ast_log(LOG_WARNING, "AMD: Unable to restore read format on '%s'\n", chan->name);
304         }
305
306         /* Free The Silence Detector DSP */
307         ast_dsp_free(silenceDetector );
308
309         return;
310 }
311
312
313 static int amd_exec(struct ast_channel *chan, void *data)
314 {
315         struct localuser *u;
316
317         LOCAL_USER_ADD(u);
318         isAnsweringMachine(chan, data);
319         LOCAL_USER_REMOVE(u);
320
321         return 0;
322 }
323
324 static void load_config(void)
325 {
326         struct ast_config *cfg;
327         char *cat;
328         struct ast_variable *var;
329
330         cfg = ast_config_load("amd.conf");
331
332         if (!cfg) {
333                 ast_log(LOG_ERROR, "Configuration file amd.conf missing.\n");
334                 return;
335         }
336
337         cat = ast_category_browse(cfg, NULL);
338
339         while (cat) {
340                 if (!strcasecmp(cat, "amd") ) {
341                         var = ast_variable_browse(cfg, cat);
342                         while (var) {
343                                 if (!strcasecmp(var->name, "initial_silence")) {
344                                         dfltInitialSilence = atoi(var->value);
345                                 } else if (!strcasecmp(var->name, "greeting")) {
346                                         dfltGreeting = atoi(var->value);
347                                 } else if (!strcasecmp(var->name, "after_greeting_silence")) {
348                                         dfltAfterGreetingSilence = atoi(var->value);
349                                 } else if (!strcasecmp(var->name, "silence_threshold")) {
350                                         dfltSilenceThreshold = atoi(var->value);
351                                 } else if (!strcasecmp(var->name, "total_analysis_time")) {
352                                         dfltTotalAnalysisTime = atoi(var->value);
353                                 } else if (!strcasecmp(var->name, "min_word_length")) {
354                                         dfltMinimumWordLength = atoi(var->value);
355                                 } else if (!strcasecmp(var->name, "between_words_silence")) {
356                                         dfltBetweenWordsSilence = atoi(var->value);
357                                 } else if (!strcasecmp(var->name, "maximum_number_of_words")) {
358                                         dfltMaximumNumberOfWords = atoi(var->value);
359                                 } else {
360                                         ast_log(LOG_WARNING, "%s: Cat:%s. Unknown keyword %s at line %d of amd.conf\n",
361                                                 app, cat, var->name, var->lineno);
362                                 }
363                                 var = var->next;
364                         }
365                 }
366                 cat = ast_category_browse(cfg, cat);
367         }
368         ast_config_destroy(cfg);
369
370         ast_verbose(VERBOSE_PREFIX_3 "AMD defaults: initialSilence [%d] greeting [%d] afterGreetingSilence [%d] "
371                 "totalAnalysisTime [%d] minimumWordLength [%d] betweenWordsSilence [%d] maximumNumberOfWords [%d] silenceThreshold [%d] \n",
372                                 dfltInitialSilence, dfltGreeting, dfltAfterGreetingSilence, dfltTotalAnalysisTime,
373                                 dfltMinimumWordLength, dfltBetweenWordsSilence, dfltMaximumNumberOfWords, dfltSilenceThreshold );
374
375         return;
376 }
377
378 int unload_module(void)
379 {
380         STANDARD_HANGUP_LOCALUSERS;
381         return ast_unregister_application(app);
382 }
383
384 int load_module(void)
385 {
386         load_config();
387         return ast_register_application(app, amd_exec, synopsis, descrip);
388 }
389
390 int reload(void)
391 {
392         load_config();
393         return 0;
394 }
395
396 char *description(void)
397 {
398         return tdesc;
399 }
400
401 int usecount(void)
402 {
403         int res;
404         STANDARD_USECOUNT(res);
405         return res;
406 }
407
408 char *key()
409 {
410         return ASTERISK_GPL_KEY;
411 }
412