app_waitforsilence.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/file.h"
  45. #include "asterisk/channel.h"
  46. #include "asterisk/pbx.h"
  47. #include "asterisk/dsp.h"
  48. #include "asterisk/module.h"
  49. #include "asterisk/format_cache.h"
  50. /*** DOCUMENTATION
  51. <application name="WaitForSilence" language="en_US">
  52. <synopsis>
  53. Waits for a specified amount of silence.
  54. </synopsis>
  55. <syntax>
  56. <parameter name="silencerequired" required="true" />
  57. <parameter name="iterations">
  58. <para>If not specified, defaults to <literal>1</literal>.</para>
  59. </parameter>
  60. <parameter name="timeout">
  61. <para>Is specified only to avoid an infinite loop in cases where silence is never achieved.</para>
  62. </parameter>
  63. </syntax>
  64. <description>
  65. <para>Waits for up to <replaceable>silencerequired</replaceable> milliseconds of silence,
  66. <replaceable>iterations</replaceable> times. An optional <replaceable>timeout</replaceable>
  67. specified the number of seconds to return after, even if we do not receive the specified amount of silence.
  68. Use <replaceable>timeout</replaceable> with caution, as it may defeat the purpose of this application, which
  69. is to wait indefinitely until silence is detected on the line. This is particularly useful for reverse-911-type
  70. call broadcast applications where you need to wait for an answering machine to complete its spiel before
  71. playing a message.</para>
  72. <para>Typically you will want to include two or more calls to WaitForSilence when dealing with an answering
  73. machine; first waiting for the spiel to finish, then waiting for the beep, etc.</para>
  74. <para>Examples:</para>
  75. <para>WaitForSilence(500,2) will wait for 1/2 second of silence, twice</para>
  76. <para>WaitForSilence(1000) will wait for 1 second of silence, once</para>
  77. <para>WaitForSilence(300,3,10) will wait for 300ms silence, 3 times, and returns after 10 sec, even if silence
  78. is not detected</para>
  79. <para>Sets the channel variable <variable>WAITSTATUS</variable> to one of these values:</para>
  80. <variablelist>
  81. <variable name="WAITSTATUS">
  82. <value name="SILENCE">
  83. if exited with silence detected.
  84. </value>
  85. <value name="TIMEOUT">
  86. if exited without silence detected after timeout.
  87. </value>
  88. </variable>
  89. </variablelist>
  90. </description>
  91. <see-also>
  92. <ref type="application">WaitForNoise</ref>
  93. </see-also>
  94. </application>
  95. <application name="WaitForNoise" language="en_US">
  96. <synopsis>
  97. Waits for a specified amount of noise.
  98. </synopsis>
  99. <syntax>
  100. <parameter name="noiserequired" required="true" />
  101. <parameter name="iterations">
  102. <para>If not specified, defaults to <literal>1</literal>.</para>
  103. </parameter>
  104. <parameter name="timeout">
  105. <para>Is specified only to avoid an infinite loop in cases where silence is never achieved.</para>
  106. </parameter>
  107. </syntax>
  108. <description>
  109. <para>Waits for up to <replaceable>noiserequired</replaceable> milliseconds of noise,
  110. <replaceable>iterations</replaceable> times. An optional <replaceable>timeout</replaceable>
  111. specified the number of seconds to return after, even if we do not receive the specified amount of noise.
  112. Use <replaceable>timeout</replaceable> with caution, as it may defeat the purpose of this application, which
  113. is to wait indefinitely until noise is detected on the line.</para>
  114. </description>
  115. <see-also>
  116. <ref type="application">WaitForSilence</ref>
  117. </see-also>
  118. </application>
  119. ***/
  120. static char *app_silence = "WaitForSilence";
  121. static char *app_noise = "WaitForNoise";
  122. static int do_waiting(struct ast_channel *chan, int timereqd, time_t waitstart, int timeout, int wait_for_silence) {
  123. struct ast_frame *f = NULL;
  124. int dsptime = 0;
  125. RAII_VAR(struct ast_format *, rfmt, NULL, ao2_cleanup);
  126. int res = 0;
  127. struct ast_dsp *sildet; /* silence detector dsp */
  128. time_t now;
  129. /*Either silence or noise calc depending on wait_for_silence flag*/
  130. int (*ast_dsp_func)(struct ast_dsp*, struct ast_frame*, int*) =
  131. wait_for_silence ? ast_dsp_silence : ast_dsp_noise;
  132. rfmt = ao2_bump(ast_channel_readformat(chan));
  133. if ((res = ast_set_read_format(chan, ast_format_slin)) < 0) {
  134. ast_log(LOG_WARNING, "Unable to set channel to linear mode, giving up\n");
  135. return -1;
  136. }
  137. /* Create the silence detector */
  138. if (!(sildet = ast_dsp_new())) {
  139. ast_log(LOG_WARNING, "Unable to create silence detector :(\n");
  140. return -1;
  141. }
  142. ast_dsp_set_threshold(sildet, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE));
  143. /* Await silence... */
  144. for (;;) {
  145. /* Start with no silence received */
  146. dsptime = 0;
  147. res = ast_waitfor(chan, timereqd);
  148. /* Must have gotten a hangup; let's exit */
  149. if (res < 0) {
  150. pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
  151. break;
  152. }
  153. /* We waited and got no frame; sounds like digital silence or a muted digital channel */
  154. if (res == 0) {
  155. dsptime = timereqd;
  156. } else {
  157. /* Looks like we did get a frame, so let's check it out */
  158. if (!(f = ast_read(chan))) {
  159. pbx_builtin_setvar_helper(chan, "WAITSTATUS", "HANGUP");
  160. break;
  161. }
  162. if (f->frametype == AST_FRAME_VOICE) {
  163. ast_dsp_func(sildet, f, &dsptime);
  164. }
  165. ast_frfree(f);
  166. }
  167. ast_debug(1, "Got %dms %s < %dms required\n", dsptime, wait_for_silence ? "silence" : "noise", timereqd);
  168. if (dsptime >= timereqd) {
  169. ast_verb(3, "Exiting with %dms %s >= %dms required\n", dsptime, wait_for_silence ? "silence" : "noise", timereqd);
  170. /* Ended happily with silence */
  171. res = 1;
  172. pbx_builtin_setvar_helper(chan, "WAITSTATUS", wait_for_silence ? "SILENCE" : "NOISE");
  173. ast_debug(1, "WAITSTATUS was set to %s\n", wait_for_silence ? "SILENCE" : "NOISE");
  174. break;
  175. }
  176. if (timeout && (difftime(time(&now), waitstart) >= timeout)) {
  177. pbx_builtin_setvar_helper(chan, "WAITSTATUS", "TIMEOUT");
  178. ast_debug(1, "WAITSTATUS was set to TIMEOUT\n");
  179. res = 0;
  180. break;
  181. }
  182. }
  183. if (rfmt && ast_set_read_format(chan, rfmt)) {
  184. ast_log(LOG_WARNING, "Unable to restore format %s to channel '%s'\n", ast_format_get_name(rfmt), ast_channel_name(chan));
  185. }
  186. ast_dsp_free(sildet);
  187. return res;
  188. }
  189. static int waitfor_exec(struct ast_channel *chan, const char *data, int wait_for_silence)
  190. {
  191. int res = 1;
  192. int timereqd = 1000;
  193. int timeout = 0;
  194. int iterations = 1, i;
  195. time_t waitstart;
  196. struct ast_silence_generator *silgen = NULL;
  197. if (ast_channel_state(chan) != AST_STATE_UP) {
  198. res = ast_answer(chan); /* Answer the channel */
  199. }
  200. if (!data || ( (sscanf(data, "%30d,%30d,%30d", &timereqd, &iterations, &timeout) != 3) &&
  201. (sscanf(data, "%30d,%30d", &timereqd, &iterations) != 2) &&
  202. (sscanf(data, "%30d", &timereqd) != 1) ) ) {
  203. ast_log(LOG_WARNING, "Using default value of 1000ms, 1 iteration, no timeout\n");
  204. }
  205. ast_verb(3, "Waiting %d time(s) for %d ms silence with %d timeout\n", iterations, timereqd, timeout);
  206. if (ast_opt_transmit_silence) {
  207. silgen = ast_channel_start_silence_generator(chan);
  208. }
  209. time(&waitstart);
  210. res = 1;
  211. for (i=0; (i<iterations) && (res == 1); i++) {
  212. res = do_waiting(chan, timereqd, waitstart, timeout, wait_for_silence);
  213. }
  214. if (silgen) {
  215. ast_channel_stop_silence_generator(chan, silgen);
  216. }
  217. if (res > 0)
  218. res = 0;
  219. return res;
  220. }
  221. static int waitforsilence_exec(struct ast_channel *chan, const char *data)
  222. {
  223. return waitfor_exec(chan, data, 1);
  224. }
  225. static int waitfornoise_exec(struct ast_channel *chan, const char *data)
  226. {
  227. return waitfor_exec(chan, data, 0);
  228. }
  229. static int unload_module(void)
  230. {
  231. int res;
  232. res = ast_unregister_application(app_silence);
  233. res |= ast_unregister_application(app_noise);
  234. return res;
  235. }
  236. static int load_module(void)
  237. {
  238. int res;
  239. res = ast_register_application_xml(app_silence, waitforsilence_exec);
  240. res |= ast_register_application_xml(app_noise, waitfornoise_exec);
  241. return res;
  242. }
  243. AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Wait For Silence");