func_talkdetect.c 13 KB

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