func_talkdetect.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Digium, Inc.
  5. *
  6. * Matt Jordan <mjordan@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Function that raises events when talking is detected on a channel
  21. *
  22. * \author Matt Jordan <mjordan@digium.com>
  23. *
  24. * \ingroup functions
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/module.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/app.h"
  35. #include "asterisk/dsp.h"
  36. #include "asterisk/audiohook.h"
  37. #include "asterisk/stasis.h"
  38. #include "asterisk/stasis_channels.h"
  39. /*** DOCUMENTATION
  40. <function name="TALK_DETECT" language="en_US">
  41. <synopsis>
  42. Raises notifications when Asterisk detects silence or talking on a channel.
  43. </synopsis>
  44. <syntax>
  45. <parameter name="action" required="true">
  46. <optionlist>
  47. <option name="remove">
  48. <para>W/O. Remove talk detection from the channel.</para>
  49. </option>
  50. <option name="set">
  51. <para>W/O. Enable TALK_DETECT and/or configure talk detection
  52. parameters. Can be called multiple times to change parameters
  53. on a channel with talk detection already enabled.</para>
  54. <argument name="dsp_silence_threshold" required="false">
  55. <para>The time in milliseconds before which a user is considered silent.</para>
  56. </argument>
  57. <argument name="dsp_talking_threshold" required="false">
  58. <para>The time in milliseconds after which a user is considered talking.</para>
  59. </argument>
  60. </option>
  61. </optionlist>
  62. </parameter>
  63. </syntax>
  64. <description>
  65. <para>The TALK_DETECT function enables events on the channel
  66. it is applied to. These events can be emited over AMI, ARI, and
  67. potentially other Asterisk modules that listen for the internal
  68. notification.</para>
  69. <para>The function has two parameters that can optionally be passed
  70. when <literal>set</literal> on a channel: <replaceable>dsp_talking_threshold</replaceable>
  71. and <replaceable>dsp_silence_threshold</replaceable>.</para>
  72. <para><replaceable>dsp_talking_threshold</replaceable> is the time in milliseconds of sound
  73. above what the dsp has established as base line silence for a user
  74. before a user is considered to be talking. By default, the value of
  75. <replaceable>silencethreshold</replaceable> from <filename>dsp.conf</filename>
  76. is used. If this value is set too tight events may be
  77. falsely triggered by variants in room noise.</para>
  78. <para>Valid values are 1 through 2^31.</para>
  79. <para><replaceable>dsp_silence_threshold</replaceable> is the time in milliseconds of sound
  80. falling within what the dsp has established as baseline silence before
  81. a user is considered be silent. If this value is set too low events
  82. indicating the user has stopped talking may get falsely sent out when
  83. the user briefly pauses during mid sentence.</para>
  84. <para>The best way to approach this option is to set it slightly above
  85. the maximum amount of ms of silence a user may generate during
  86. natural speech.</para>
  87. <para>By default this value is 2500ms. Valid values are 1
  88. through 2^31.</para>
  89. <para>Example:</para>
  90. <para>same => n,Set(TALK_DETECT(set)=) ; Enable talk detection</para>
  91. <para>same => n,Set(TALK_DETECT(set)=1200) ; Update existing talk detection's silence threshold to 1200 ms</para>
  92. <para>same => n,Set(TALK_DETECT(remove)=) ; Remove talk detection</para>
  93. <para>same => n,Set(TALK_DETECT(set)=,128) ; Enable and set talk threshold to 128</para>
  94. <para>This function will set the following variables:</para>
  95. <note>
  96. <para>The TALK_DETECT function uses an audiohook to inspect the
  97. voice media frames on a channel. Other functions, such as JITTERBUFFER,
  98. DENOISE, and AGC use a similar mechanism. Audiohooks are processed
  99. in the order in which they are placed on the channel. As such,
  100. it typically makes sense to place functions that modify the voice
  101. media data prior to placing the TALK_DETECT function, as this will
  102. yield better results.</para>
  103. <para>Example:</para>
  104. <para>same => n,Set(DENOISE(rx)=on) ; Denoise received audio</para>
  105. <para>same => n,Set(TALK_DETECT(set)=) ; Perform talk detection on the denoised received audio</para>
  106. </note>
  107. </description>
  108. </function>
  109. ***/
  110. #define DEFAULT_SILENCE_THRESHOLD 2500
  111. /*! \brief Private data structure used with the function's datastore */
  112. struct talk_detect_params {
  113. /*! The audiohook for the function */
  114. struct ast_audiohook audiohook;
  115. /*! Our threshold above which we consider someone talking */
  116. int dsp_talking_threshold;
  117. /*! How long we'll wait before we decide someone is silent */
  118. int dsp_silence_threshold;
  119. /*! Whether or not the user is currently talking */
  120. int talking;
  121. /*! The time the current burst of talking started */
  122. struct timeval talking_start;
  123. /*! The DSP used to do the heavy lifting */
  124. struct ast_dsp *dsp;
  125. };
  126. /*! \internal \brief Destroy the datastore */
  127. static void datastore_destroy_cb(void *data) {
  128. struct talk_detect_params *td_params = data;
  129. ast_audiohook_destroy(&td_params->audiohook);
  130. if (td_params->dsp) {
  131. ast_dsp_free(td_params->dsp);
  132. }
  133. ast_free(data);
  134. }
  135. /*! \brief The channel datastore the function uses to store state */
  136. static const struct ast_datastore_info talk_detect_datastore = {
  137. .type = "talk_detect",
  138. .destroy = datastore_destroy_cb
  139. };
  140. /*! \internal \brief An audiohook modification callback
  141. *
  142. * This processes the read side of a channel's voice data to see if
  143. * they are talking
  144. *
  145. * \note We don't actually modify the audio, so this function always
  146. * returns a 'failure' indicating that it didn't modify the data
  147. */
  148. static int talk_detect_audiohook_cb(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
  149. {
  150. int total_silence;
  151. int update_talking = 0;
  152. struct ast_datastore *datastore;
  153. struct talk_detect_params *td_params;
  154. struct stasis_message *message;
  155. if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
  156. return 1;
  157. }
  158. if (direction != AST_AUDIOHOOK_DIRECTION_READ) {
  159. return 1;
  160. }
  161. if (frame->frametype != AST_FRAME_VOICE) {
  162. return 1;
  163. }
  164. if (!(datastore = ast_channel_datastore_find(chan, &talk_detect_datastore, NULL))) {
  165. return 1;
  166. }
  167. td_params = datastore->data;
  168. ast_dsp_silence(td_params->dsp, frame, &total_silence);
  169. if (total_silence < td_params->dsp_silence_threshold) {
  170. if (!td_params->talking) {
  171. update_talking = 1;
  172. td_params->talking_start = ast_tvnow();
  173. }
  174. td_params->talking = 1;
  175. } else {
  176. if (td_params->talking) {
  177. update_talking = 1;
  178. }
  179. td_params->talking = 0;
  180. }
  181. if (update_talking) {
  182. struct ast_json *blob = NULL;
  183. if (!td_params->talking) {
  184. int64_t diff_ms = ast_tvdiff_ms(ast_tvnow(), td_params->talking_start);
  185. diff_ms -= td_params->dsp_silence_threshold;
  186. blob = ast_json_pack("{s: i}", "duration", diff_ms);
  187. if (!blob) {
  188. return 1;
  189. }
  190. }
  191. ast_verb(4, "%s is now %s\n", ast_channel_name(chan),
  192. td_params->talking ? "talking" : "silent");
  193. message = ast_channel_blob_create_from_cache(ast_channel_uniqueid(chan),
  194. td_params->talking ? ast_channel_talking_start() : ast_channel_talking_stop(),
  195. blob);
  196. if (message) {
  197. stasis_publish(ast_channel_topic(chan), message);
  198. }
  199. ast_json_unref(blob);
  200. }
  201. return 1;
  202. }
  203. /*! \internal \brief Disable talk detection on the channel */
  204. static int remove_talk_detect(struct ast_channel *chan)
  205. {
  206. struct ast_datastore *datastore = NULL;
  207. struct talk_detect_params *td_params;
  208. SCOPED_CHANNELLOCK(chan_lock, chan);
  209. datastore = ast_channel_datastore_find(chan, &talk_detect_datastore, NULL);
  210. if (!datastore) {
  211. ast_log(AST_LOG_WARNING, "Cannot remove TALK_DETECT from %s: TALK_DETECT not currently enabled\n",
  212. ast_channel_name(chan));
  213. return -1;
  214. }
  215. td_params = datastore->data;
  216. if (ast_audiohook_remove(chan, &td_params->audiohook)) {
  217. ast_log(AST_LOG_WARNING, "Failed to remove TALK_DETECT audiohook from channel %s\n",
  218. ast_channel_name(chan));
  219. return -1;
  220. }
  221. if (ast_channel_datastore_remove(chan, datastore)) {
  222. ast_log(AST_LOG_WARNING, "Failed to remove TALK_DETECT datastore from channel %s\n",
  223. ast_channel_name(chan));
  224. return -1;
  225. }
  226. ast_datastore_free(datastore);
  227. return 0;
  228. }
  229. /*! \internal \brief Enable talk detection on the channel */
  230. static int set_talk_detect(struct ast_channel *chan, int dsp_silence_threshold, int dsp_talking_threshold)
  231. {
  232. struct ast_datastore *datastore = NULL;
  233. struct talk_detect_params *td_params;
  234. SCOPED_CHANNELLOCK(chan_lock, chan);
  235. datastore = ast_channel_datastore_find(chan, &talk_detect_datastore, NULL);
  236. if (!datastore) {
  237. datastore = ast_datastore_alloc(&talk_detect_datastore, NULL);
  238. if (!datastore) {
  239. return -1;
  240. }
  241. td_params = ast_calloc(1, sizeof(*td_params));
  242. if (!td_params) {
  243. ast_datastore_free(datastore);
  244. return -1;
  245. }
  246. ast_audiohook_init(&td_params->audiohook,
  247. AST_AUDIOHOOK_TYPE_MANIPULATE,
  248. "TALK_DETECT",
  249. AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
  250. td_params->audiohook.manipulate_callback = talk_detect_audiohook_cb;
  251. ast_set_flag(&td_params->audiohook, AST_AUDIOHOOK_TRIGGER_READ);
  252. td_params->dsp = ast_dsp_new_with_rate(ast_format_get_sample_rate(ast_channel_rawreadformat(chan)));
  253. if (!td_params->dsp) {
  254. ast_datastore_free(datastore);
  255. ast_free(td_params);
  256. return -1;
  257. }
  258. datastore->data = td_params;
  259. ast_channel_datastore_add(chan, datastore);
  260. ast_audiohook_attach(chan, &td_params->audiohook);
  261. } else {
  262. /* Talk detection already enabled; update existing settings */
  263. td_params = datastore->data;
  264. }
  265. td_params->dsp_talking_threshold = dsp_talking_threshold;
  266. td_params->dsp_silence_threshold = dsp_silence_threshold;
  267. ast_dsp_set_threshold(td_params->dsp, td_params->dsp_talking_threshold);
  268. return 0;
  269. }
  270. /*! \internal \brief TALK_DETECT write function callback */
  271. static int talk_detect_fn_write(struct ast_channel *chan, const char *function, char *data, const char *value)
  272. {
  273. int res;
  274. if (!chan) {
  275. return -1;
  276. }
  277. if (ast_strlen_zero(data)) {
  278. ast_log(AST_LOG_WARNING, "TALK_DETECT requires an argument\n");
  279. return -1;
  280. }
  281. if (!strcasecmp(data, "set")) {
  282. int dsp_silence_threshold = DEFAULT_SILENCE_THRESHOLD;
  283. int dsp_talking_threshold = ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE);
  284. if (!ast_strlen_zero(value)) {
  285. char *parse = ast_strdupa(value);
  286. AST_DECLARE_APP_ARGS(args,
  287. AST_APP_ARG(silence_threshold);
  288. AST_APP_ARG(talking_threshold);
  289. );
  290. AST_STANDARD_APP_ARGS(args, parse);
  291. if (!ast_strlen_zero(args.silence_threshold)) {
  292. if (sscanf(args.silence_threshold, "%30d", &dsp_silence_threshold) != 1) {
  293. ast_log(AST_LOG_WARNING, "Failed to parse %s for dsp_silence_threshold\n",
  294. args.silence_threshold);
  295. return -1;
  296. }
  297. if (dsp_silence_threshold < 1) {
  298. ast_log(AST_LOG_WARNING, "Invalid value %d for dsp_silence_threshold\n",
  299. dsp_silence_threshold);
  300. return -1;
  301. }
  302. }
  303. if (!ast_strlen_zero(args.talking_threshold)) {
  304. if (sscanf(args.talking_threshold, "%30d", &dsp_talking_threshold) != 1) {
  305. ast_log(AST_LOG_WARNING, "Failed to parse %s for dsp_talking_threshold\n",
  306. args.talking_threshold);
  307. return -1;
  308. }
  309. if (dsp_talking_threshold < 1) {
  310. ast_log(AST_LOG_WARNING, "Invalid value %d for dsp_talking_threshold\n",
  311. dsp_silence_threshold);
  312. return -1;
  313. }
  314. }
  315. }
  316. res = set_talk_detect(chan, dsp_silence_threshold, dsp_talking_threshold);
  317. } else if (!strcasecmp(data, "remove")) {
  318. res = remove_talk_detect(chan);
  319. } else {
  320. ast_log(AST_LOG_WARNING, "TALK_DETECT: unknown option %s\n", data);
  321. res = -1;
  322. }
  323. return res;
  324. }
  325. /*! \brief Definition of the TALK_DETECT function */
  326. static struct ast_custom_function talk_detect_function = {
  327. .name = "TALK_DETECT",
  328. .write = talk_detect_fn_write,
  329. };
  330. /*! \internal \brief Unload the module */
  331. static int unload_module(void)
  332. {
  333. int res = 0;
  334. res |= ast_custom_function_unregister(&talk_detect_function);
  335. return res;
  336. }
  337. /*! \internal \brief Load the module */
  338. static int load_module(void)
  339. {
  340. int res = 0;
  341. res |= ast_custom_function_register(&talk_detect_function);
  342. return res ? AST_MODULE_LOAD_FAILURE : AST_MODULE_LOAD_SUCCESS;
  343. }
  344. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Talk detection dialplan function");