app_waitforsilence.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * WaitForSilence Application by David C. Troy <dave@popvox.com>
  7. * Version 1.11 2006-06-29
  8. *
  9. * Mark Spencer <markster@digium.com>
  10. *
  11. * See http://www.asterisk.org for more information about
  12. * the Asterisk project. Please do not directly contact
  13. * any of the maintainers of this project for assistance;
  14. * the project provides a web site, mailing lists and IRC
  15. * channels for your use.
  16. *
  17. * This program is free software, distributed under the terms of
  18. * the GNU General Public License Version 2. See the LICENSE file
  19. * at the top of the source tree.
  20. */
  21. /*! \file
  22. *
  23. * \brief Wait for Silence
  24. * - Waits for up to 'x' milliseconds of silence, 'y' times \n
  25. * - WaitForSilence(500,2) will wait for 1/2 second of silence, twice \n
  26. * - WaitForSilence(1000,1) will wait for 1 second of silence, once \n
  27. * - WaitForSilence(300,3,10) will wait for 300ms of silence, 3 times, and return after 10sec \n
  28. *
  29. * \author David C. Troy <dave@popvox.com>
  30. *
  31. * \brief Wait For Noise
  32. * The same as Wait For Silence but listenes noise on the chennel that is above \n
  33. * the pre-configured silence threshold from dsp.conf
  34. *
  35. * \author Philipp Skadorov <skadorov@yahoo.com>
  36. *
  37. * \ingroup applications
  38. */
  39. /*** MODULEINFO
  40. <support_level>extended</support_level>
  41. ***/
  42. #include "asterisk.h"
  43. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  44. #include "asterisk/app.h"
  45. #include "asterisk/file.h"
  46. #include "asterisk/channel.h"
  47. #include "asterisk/pbx.h"
  48. #include "asterisk/dsp.h"
  49. #include "asterisk/module.h"
  50. #include "asterisk/format_cache.h"
  51. /*** DOCUMENTATION
  52. <application name="WaitForSilence" language="en_US">
  53. <synopsis>
  54. Waits for a specified amount of silence.
  55. </synopsis>
  56. <syntax>
  57. <parameter name="silencerequired">
  58. <para>If not specified, defaults to <literal>1000</literal> milliseconds.</para>
  59. </parameter>
  60. <parameter name="iterations">
  61. <para>If not specified, defaults to <literal>1</literal>.</para>
  62. </parameter>
  63. <parameter name="timeout">
  64. <para>Is specified only to avoid an infinite loop in cases where silence is never achieved.</para>
  65. </parameter>
  66. </syntax>
  67. <description>
  68. <para>Waits for up to <replaceable>silencerequired</replaceable> milliseconds of silence,
  69. <replaceable>iterations</replaceable> times. An optional <replaceable>timeout</replaceable>
  70. specified the number of seconds to return after, even if we do not receive the specified amount of silence.
  71. Use <replaceable>timeout</replaceable> with caution, as it may defeat the purpose of this application, which
  72. is to wait indefinitely until silence is detected on the line. This is particularly useful for reverse-911-type
  73. call broadcast applications where you need to wait for an answering machine to complete its spiel before
  74. playing a message.</para>
  75. <para>Typically you will want to include two or more calls to WaitForSilence when dealing with an answering
  76. machine; first waiting for the spiel to finish, then waiting for the beep, etc.</para>
  77. <para>Examples:</para>
  78. <para>WaitForSilence(500,2) will wait for 1/2 second of silence, twice</para>
  79. <para>WaitForSilence(1000) will wait for 1 second of silence, once</para>
  80. <para>WaitForSilence(300,3,10) will wait for 300ms silence, 3 times, and returns after 10 sec, even if silence
  81. is not detected</para>
  82. <para>Sets the channel variable <variable>WAITSTATUS</variable> to one of these values:</para>
  83. <variablelist>
  84. <variable name="WAITSTATUS">
  85. <value name="SILENCE">
  86. if exited with silence detected.
  87. </value>
  88. <value name="TIMEOUT">
  89. if exited without silence detected after timeout.
  90. </value>
  91. </variable>
  92. </variablelist>
  93. </description>
  94. <see-also>
  95. <ref type="application">WaitForNoise</ref>
  96. </see-also>
  97. </application>
  98. <application name="WaitForNoise" language="en_US">
  99. <synopsis>
  100. Waits for a specified amount of noise.
  101. </synopsis>
  102. <syntax>
  103. <parameter name="noiserequired">
  104. <para>If not specified, defaults to <literal>1000</literal> milliseconds.</para>
  105. </parameter>
  106. <parameter name="iterations">
  107. <para>If not specified, defaults to <literal>1</literal>.</para>
  108. </parameter>
  109. <parameter name="timeout">
  110. <para>Is specified only to avoid an infinite loop in cases where silence is never achieved.</para>
  111. </parameter>
  112. </syntax>
  113. <description>
  114. <para>Waits for up to <replaceable>noiserequired</replaceable> milliseconds of noise,
  115. <replaceable>iterations</replaceable> times. An optional <replaceable>timeout</replaceable>
  116. specified the number of seconds to return after, even if we do not receive the specified amount of noise.
  117. Use <replaceable>timeout</replaceable> with caution, as it may defeat the purpose of this application, which
  118. is to wait indefinitely until noise is detected on the line.</para>
  119. </description>
  120. <see-also>
  121. <ref type="application">WaitForSilence</ref>
  122. </see-also>
  123. </application>
  124. ***/
  125. static char *app_silence = "WaitForSilence";
  126. static char *app_noise = "WaitForNoise";
  127. struct wait_type {
  128. const char *name;
  129. const char *status;
  130. int stop_on_frame_timeout;
  131. int (*func)(struct ast_dsp *, struct ast_frame *, int *);
  132. };
  133. static const struct wait_type wait_for_silence = {
  134. .name = "silence",
  135. .status = "SILENCE",
  136. .stop_on_frame_timeout = 1,
  137. .func = ast_dsp_silence,
  138. };
  139. static const struct wait_type wait_for_noise = {
  140. .name = "noise",
  141. .status = "NOISE",
  142. .stop_on_frame_timeout = 0,
  143. .func = ast_dsp_noise,
  144. };
  145. static int do_waiting(struct ast_channel *chan, int timereqd, time_t waitstart, int timeout, const struct wait_type *wait_for)
  146. {
  147. RAII_VAR(struct ast_format *, rfmt, NULL, ao2_cleanup);
  148. int res;
  149. struct ast_dsp *sildet;
  150. rfmt = ao2_bump(ast_channel_readformat(chan));
  151. if ((res = ast_set_read_format(chan, ast_format_slin)) < 0) {
  152. ast_log(LOG_WARNING, "Unable to set channel to linear mode, giving up\n");
  153. return -1;
  154. }
  155. /* Create the silence detector */
  156. if (!(sildet = ast_dsp_new())) {
  157. ast_log(LOG_WARNING, "Unable to create silence detector\n");
  158. return -1;
  159. }
  160. ast_dsp_set_threshold(sildet, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE));
  161. for (;;) {
  162. int dsptime = 0;
  163. res = ast_waitfor(chan, timereqd);
  164. /* Must have gotten a hangup; let's exit */
  165. if (res < 0) {
  166. pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
  167. break;
  168. }
  169. /* We waited and got no frame; sounds like digital silence or a muted digital channel */
  170. if (res == 0) {
  171. if (wait_for->stop_on_frame_timeout) {
  172. dsptime = timereqd;
  173. }
  174. } else {
  175. /* Looks like we did get a frame, so let's check it out */
  176. struct ast_frame *f = ast_read(chan);
  177. if (!f) {
  178. pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
  179. break;
  180. }
  181. if (f->frametype == AST_FRAME_VOICE) {
  182. wait_for->func(sildet, f, &dsptime);
  183. }
  184. ast_frfree(f);
  185. }
  186. ast_debug(1, "Got %dms of %s < %dms required\n", dsptime, wait_for->name, timereqd);
  187. if (dsptime >= timereqd) {
  188. ast_verb(3, "Exiting with %dms of %s >= %dms required\n", dsptime, wait_for->name, timereqd);
  189. pbx_builtin_setvar_helper(chan, "WAITSTATUS", wait_for->status);
  190. ast_debug(1, "WAITSTATUS was set to %s\n", wait_for->status);
  191. res = 1;
  192. break;
  193. }
  194. if (timeout && difftime(time(NULL), waitstart) >= timeout) {
  195. pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
  196. ast_debug(1, "WAITSTATUS was set to TIMEOUT\n");
  197. res = 0;
  198. break;
  199. }
  200. }
  201. if (rfmt && ast_set_read_format(chan, rfmt)) {
  202. ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_format_get_name(rfmt), ast_channel_name(chan));
  203. }
  204. ast_dsp_free(sildet);
  205. return res;
  206. }
  207. static int waitfor_exec(struct ast_channel *chan, const char *data, const struct wait_type *wait_for)
  208. {
  209. int res = 1;
  210. int timereqd = 1000;
  211. int timeout = 0;
  212. int iterations = 1, i;
  213. time_t waitstart;
  214. char *parse;
  215. struct ast_silence_generator *silgen = NULL;
  216. AST_DECLARE_APP_ARGS(args,
  217. AST_APP_ARG(timereqd);
  218. AST_APP_ARG(iterations);
  219. AST_APP_ARG(timeout);
  220. );
  221. parse = ast_strdupa(data);
  222. AST_STANDARD_APP_ARGS(args, parse);
  223. if (!ast_strlen_zero(args.timereqd)) {
  224. if (sscanf(args.timereqd, "%30d", &timereqd) != 1 || timereqd < 0) {
  225. ast_log(LOG_ERROR, "Argument '%srequired' must be an integer greater than or equal to zero.\n",
  226. wait_for->name);
  227. return -1;
  228. }
  229. }
  230. if (!ast_strlen_zero(args.iterations)) {
  231. if (sscanf(args.iterations, "%30d", &iterations) != 1 || iterations < 1) {
  232. ast_log(LOG_ERROR, "Argument 'iterations' must be an integer greater than 0.\n");
  233. return -1;
  234. }
  235. }
  236. if (!ast_strlen_zero(args.timeout)) {
  237. if (sscanf(args.timeout, "%30d", &timeout) != 1 || timeout < 0) {
  238. ast_log(LOG_ERROR, "Argument 'timeout' must be an integer greater than or equal to zero.\n");
  239. return -1;
  240. }
  241. }
  242. if (ast_channel_state(chan) != AST_STATE_UP) {
  243. ast_answer(chan); /* Answer the channel */
  244. }
  245. ast_verb(3, "Waiting %d time(s) for %dms of %s with %ds timeout\n",
  246. iterations, timereqd, wait_for->name, timeout);
  247. if (ast_opt_transmit_silence) {
  248. silgen = ast_channel_start_silence_generator(chan);
  249. }
  250. time(&waitstart);
  251. for (i = 0; i < iterations && res == 1; i++) {
  252. res = do_waiting(chan, timereqd, waitstart, timeout, wait_for);
  253. }
  254. if (silgen) {
  255. ast_channel_stop_silence_generator(chan, silgen);
  256. }
  257. return res > 0 ? 0 : res;
  258. }
  259. static int waitforsilence_exec(struct ast_channel *chan, const char *data)
  260. {
  261. return waitfor_exec(chan, data, &wait_for_silence);
  262. }
  263. static int waitfornoise_exec(struct ast_channel *chan, const char *data)
  264. {
  265. return waitfor_exec(chan, data, &wait_for_noise);
  266. }
  267. static int unload_module(void)
  268. {
  269. int res;
  270. res = ast_unregister_application(app_silence);
  271. res |= ast_unregister_application(app_noise);
  272. return res;
  273. }
  274. static int load_module(void)
  275. {
  276. int res;
  277. res = ast_register_application_xml(app_silence, waitforsilence_exec);
  278. res |= ast_register_application_xml(app_noise, waitfornoise_exec);
  279. return res;
  280. }
  281. AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Wait For Silence/Noise");