app_talkdetect.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Playback a file with audio detect
  5. *
  6. * Copyright (C) 2004, Digium, Inc.
  7. *
  8. * Mark Spencer <markster@digium.com>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/lock.h>
  14. #include <asterisk/file.h>
  15. #include <asterisk/logger.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/pbx.h>
  18. #include <asterisk/module.h>
  19. #include <asterisk/translate.h>
  20. #include <asterisk/utils.h>
  21. #include <asterisk/dsp.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. static char *tdesc = "Playback with Talk Detection";
  25. static char *app = "BackgroundDetect";
  26. static char *synopsis = "Background a file with talk detect";
  27. static char *descrip =
  28. " BackgroundDetect(filename[|sil[|min|[max]]]): Plays back a given\n"
  29. "filename, waiting for interruption from a given digit (the digit must\n"
  30. "start the beginning of a valid extension, or it will be ignored).\n"
  31. "During the playback of the file, audio is monitored in the receive\n"
  32. "direction, and if a period of non-silence which is greater than 'min' ms\n"
  33. "yet less than 'max' ms is followed by silence for at least 'sil' ms then\n"
  34. "the audio playback is aborted and processing jumps to the 'talk' extension\n"
  35. "if available. If unspecified, sil, min, and max default to 1000, 100, and\n"
  36. "infinity respectively. Returns -1 on hangup, and 0 on successful playback\n"
  37. "completion with no exit conditions.\n";
  38. STANDARD_LOCAL_USER;
  39. LOCAL_USER_DECL;
  40. static int background_detect_exec(struct ast_channel *chan, void *data)
  41. {
  42. int res = 0;
  43. struct localuser *u;
  44. char tmp[256];
  45. char *options;
  46. char *stringp;
  47. struct ast_frame *fr;
  48. int notsilent=0;
  49. struct timeval start = { 0, 0}, end = {0, 0};
  50. int sil = 1000;
  51. int min = 100;
  52. int max = -1;
  53. int x;
  54. int origrformat=0;
  55. struct ast_dsp *dsp;
  56. if (!data || ast_strlen_zero((char *)data)) {
  57. ast_log(LOG_WARNING, "BackgroundDetect requires an argument (filename)\n");
  58. return -1;
  59. }
  60. strncpy(tmp, (char *)data, sizeof(tmp)-1);
  61. stringp=tmp;
  62. strsep(&stringp, "|");
  63. options = strsep(&stringp, "|");
  64. if (options) {
  65. if ((sscanf(options, "%d", &x) == 1) && (x > 0))
  66. sil = x;
  67. options = strsep(&stringp, "|");
  68. if (options) {
  69. if ((sscanf(options, "%d", &x) == 1) && (x > 0))
  70. min = x;
  71. options = strsep(&stringp, "|");
  72. if (options) {
  73. if ((sscanf(options, "%d", &x) == 1) && (x > 0))
  74. max = x;
  75. }
  76. }
  77. }
  78. ast_log(LOG_DEBUG, "Preparing detect of '%s', sil=%d,min=%d,max=%d\n",
  79. tmp, sil, min, max);
  80. LOCAL_USER_ADD(u);
  81. if (chan->_state != AST_STATE_UP) {
  82. /* Otherwise answer unless we're supposed to send this while on-hook */
  83. res = ast_answer(chan);
  84. }
  85. if (!res) {
  86. origrformat = chan->readformat;
  87. if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)))
  88. ast_log(LOG_WARNING, "Unable to set read format to linear!\n");
  89. }
  90. if (!(dsp = ast_dsp_new())) {
  91. ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
  92. res = -1;
  93. }
  94. if (!res) {
  95. ast_stopstream(chan);
  96. res = ast_streamfile(chan, tmp, chan->language);
  97. if (!res) {
  98. while(chan->stream) {
  99. res = ast_sched_wait(chan->sched);
  100. if ((res < 0) && !chan->timingfunc) {
  101. res = 0;
  102. break;
  103. }
  104. if (res < 0)
  105. res = 1000;
  106. res = ast_waitfor(chan, res);
  107. if (res < 0) {
  108. ast_log(LOG_WARNING, "Waitfor failed on %s\n", chan->name);
  109. break;
  110. } else if (res > 0) {
  111. fr = ast_read(chan);
  112. if (!fr) {
  113. res = -1;
  114. break;
  115. } else if (fr->frametype == AST_FRAME_DTMF) {
  116. char t[2];
  117. t[0] = fr->subclass;
  118. t[1] = '\0';
  119. if (ast_canmatch_extension(chan, chan->context, t, 1, chan->callerid)) {
  120. /* They entered a valid extension, or might be anyhow */
  121. res = fr->subclass;
  122. ast_frfree(fr);
  123. break;
  124. }
  125. } else if ((fr->frametype == AST_FRAME_VOICE) && (fr->subclass == AST_FORMAT_SLINEAR)) {
  126. int totalsilence;
  127. int ms;
  128. res = ast_dsp_silence(dsp, fr, &totalsilence);
  129. if (res && (totalsilence > sil)) {
  130. /* We've been quiet a little while */
  131. if (notsilent) {
  132. /* We had heard some talking */
  133. gettimeofday(&end, NULL);
  134. ms = (end.tv_sec - start.tv_sec) * 1000;
  135. ms += (end.tv_usec - start.tv_usec) / 1000;
  136. ms -= sil;
  137. if (ms < 0)
  138. ms = 0;
  139. if ((ms > min) && ((max < 0) || (ms < max))) {
  140. ast_log(LOG_DEBUG, "Found qualified token of %d ms\n", ms);
  141. if (ast_exists_extension(chan, chan->context, "talk", 1, chan->callerid)) {
  142. strncpy(chan->exten, "talk", sizeof(chan->exten) -1 );
  143. chan->priority = 0;
  144. }
  145. res = 0;
  146. ast_frfree(fr);
  147. break;
  148. } else
  149. ast_log(LOG_DEBUG, "Found unqualified token of %d ms\n", ms);
  150. notsilent = 0;
  151. }
  152. } else {
  153. if (!notsilent) {
  154. /* Heard some audio, mark the begining of the token */
  155. gettimeofday(&start, NULL);
  156. ast_log(LOG_DEBUG, "Start of voice token!\n");
  157. notsilent = 1;
  158. }
  159. }
  160. }
  161. ast_frfree(fr);
  162. }
  163. ast_sched_runq(chan->sched);
  164. }
  165. ast_stopstream(chan);
  166. } else {
  167. ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char *)data);
  168. res = 0;
  169. }
  170. }
  171. if (res > -1) {
  172. if (origrformat && ast_set_read_format(chan, origrformat)) {
  173. ast_log(LOG_WARNING, "Failed to restore read format for %s to %s\n",
  174. chan->name, ast_getformatname(origrformat));
  175. }
  176. }
  177. if (dsp)
  178. ast_dsp_free(dsp);
  179. LOCAL_USER_REMOVE(u);
  180. return res;
  181. }
  182. int unload_module(void)
  183. {
  184. STANDARD_HANGUP_LOCALUSERS;
  185. return ast_unregister_application(app);
  186. }
  187. int load_module(void)
  188. {
  189. return ast_register_application(app, background_detect_exec, synopsis, descrip);
  190. }
  191. char *description(void)
  192. {
  193. return tdesc;
  194. }
  195. int usecount(void)
  196. {
  197. int res;
  198. STANDARD_USECOUNT(res);
  199. return res;
  200. }
  201. char *key()
  202. {
  203. return ASTERISK_GPL_KEY;
  204. }