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. ao2_ref(message, -1);
  199. }
  200. ast_json_unref(blob);
  201. }
  202. return 1;
  203. }
  204. /*! \internal \brief Disable talk detection on the channel */
  205. static int remove_talk_detect(struct ast_channel *chan)
  206. {
  207. struct ast_datastore *datastore = NULL;
  208. struct talk_detect_params *td_params;
  209. SCOPED_CHANNELLOCK(chan_lock, chan);
  210. datastore = ast_channel_datastore_find(chan, &talk_detect_datastore, NULL);
  211. if (!datastore) {
  212. ast_log(AST_LOG_WARNING, "Cannot remove TALK_DETECT from %s: TALK_DETECT not currently enabled\n",
  213. ast_channel_name(chan));
  214. return -1;
  215. }
  216. td_params = datastore->data;
  217. if (ast_audiohook_remove(chan, &td_params->audiohook)) {
  218. ast_log(AST_LOG_WARNING, "Failed to remove TALK_DETECT audiohook from channel %s\n",
  219. ast_channel_name(chan));
  220. return -1;
  221. }
  222. if (ast_channel_datastore_remove(chan, datastore)) {
  223. ast_log(AST_LOG_WARNING, "Failed to remove TALK_DETECT datastore from channel %s\n",
  224. ast_channel_name(chan));
  225. return -1;
  226. }
  227. ast_datastore_free(datastore);
  228. return 0;
  229. }
  230. /*! \internal \brief Enable talk detection on the channel */
  231. static int set_talk_detect(struct ast_channel *chan, int dsp_silence_threshold, int dsp_talking_threshold)
  232. {
  233. struct ast_datastore *datastore = NULL;
  234. struct talk_detect_params *td_params;
  235. SCOPED_CHANNELLOCK(chan_lock, chan);
  236. datastore = ast_channel_datastore_find(chan, &talk_detect_datastore, NULL);
  237. if (!datastore) {
  238. datastore = ast_datastore_alloc(&talk_detect_datastore, NULL);
  239. if (!datastore) {
  240. return -1;
  241. }
  242. td_params = ast_calloc(1, sizeof(*td_params));
  243. if (!td_params) {
  244. ast_datastore_free(datastore);
  245. return -1;
  246. }
  247. ast_audiohook_init(&td_params->audiohook,
  248. AST_AUDIOHOOK_TYPE_MANIPULATE,
  249. "TALK_DETECT",
  250. AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
  251. td_params->audiohook.manipulate_callback = talk_detect_audiohook_cb;
  252. ast_set_flag(&td_params->audiohook, AST_AUDIOHOOK_TRIGGER_READ);
  253. td_params->dsp = ast_dsp_new_with_rate(ast_format_get_sample_rate(ast_channel_rawreadformat(chan)));
  254. if (!td_params->dsp) {
  255. ast_datastore_free(datastore);
  256. ast_free(td_params);
  257. return -1;
  258. }
  259. datastore->data = td_params;
  260. ast_channel_datastore_add(chan, datastore);
  261. ast_audiohook_attach(chan, &td_params->audiohook);
  262. } else {
  263. /* Talk detection already enabled; update existing settings */
  264. td_params = datastore->data;
  265. }
  266. td_params->dsp_talking_threshold = dsp_talking_threshold;
  267. td_params->dsp_silence_threshold = dsp_silence_threshold;
  268. ast_dsp_set_threshold(td_params->dsp, td_params->dsp_talking_threshold);
  269. return 0;
  270. }
  271. /*! \internal \brief TALK_DETECT write function callback */
  272. static int talk_detect_fn_write(struct ast_channel *chan, const char *function, char *data, const char *value)
  273. {
  274. int res;
  275. if (!chan) {
  276. return -1;
  277. }
  278. if (ast_strlen_zero(data)) {
  279. ast_log(AST_LOG_WARNING, "TALK_DETECT requires an argument\n");
  280. return -1;
  281. }
  282. if (!strcasecmp(data, "set")) {
  283. int dsp_silence_threshold = DEFAULT_SILENCE_THRESHOLD;
  284. int dsp_talking_threshold = ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE);
  285. if (!ast_strlen_zero(value)) {
  286. char *parse = ast_strdupa(value);
  287. AST_DECLARE_APP_ARGS(args,
  288. AST_APP_ARG(silence_threshold);
  289. AST_APP_ARG(talking_threshold);
  290. );
  291. AST_STANDARD_APP_ARGS(args, parse);
  292. if (!ast_strlen_zero(args.silence_threshold)) {
  293. if (sscanf(args.silence_threshold, "%30d", &dsp_silence_threshold) != 1) {
  294. ast_log(AST_LOG_WARNING, "Failed to parse %s for dsp_silence_threshold\n",
  295. args.silence_threshold);
  296. return -1;
  297. }
  298. if (dsp_silence_threshold < 1) {
  299. ast_log(AST_LOG_WARNING, "Invalid value %d for dsp_silence_threshold\n",
  300. dsp_silence_threshold);
  301. return -1;
  302. }
  303. }
  304. if (!ast_strlen_zero(args.talking_threshold)) {
  305. if (sscanf(args.talking_threshold, "%30d", &dsp_talking_threshold) != 1) {
  306. ast_log(AST_LOG_WARNING, "Failed to parse %s for dsp_talking_threshold\n",
  307. args.talking_threshold);
  308. return -1;
  309. }
  310. if (dsp_talking_threshold < 1) {
  311. ast_log(AST_LOG_WARNING, "Invalid value %d for dsp_talking_threshold\n",
  312. dsp_silence_threshold);
  313. return -1;
  314. }
  315. }
  316. }
  317. res = set_talk_detect(chan, dsp_silence_threshold, dsp_talking_threshold);
  318. } else if (!strcasecmp(data, "remove")) {
  319. res = remove_talk_detect(chan);
  320. } else {
  321. ast_log(AST_LOG_WARNING, "TALK_DETECT: unknown option %s\n", data);
  322. res = -1;
  323. }
  324. return res;
  325. }
  326. /*! \brief Definition of the TALK_DETECT function */
  327. static struct ast_custom_function talk_detect_function = {
  328. .name = "TALK_DETECT",
  329. .write = talk_detect_fn_write,
  330. };
  331. /*! \internal \brief Unload the module */
  332. static int unload_module(void)
  333. {
  334. int res = 0;
  335. res |= ast_custom_function_unregister(&talk_detect_function);
  336. return res;
  337. }
  338. /*! \internal \brief Load the module */
  339. static int load_module(void)
  340. {
  341. int res = 0;
  342. res |= ast_custom_function_register(&talk_detect_function);
  343. return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
  344. }
  345. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Talk detection dialplan function");