app_waitforsilence.c 9.0 KB

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