res_mutestream.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2009, Olle E. Johansson
  5. *
  6. * Olle E. Johansson <oej@edvina.net>
  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 MUTESTREAM audiohooks
  21. *
  22. * \author Olle E. Johansson <oej@edvina.net>
  23. *
  24. * \ingroup functions
  25. *
  26. * \note This module only handles audio streams today, but can easily be appended to also
  27. * zero out text streams if there's an application for it.
  28. * When we know and understands what happens if we zero out video, we can do that too.
  29. */
  30. /*** MODULEINFO
  31. <support_level>core</support_level>
  32. ***/
  33. #include "asterisk.h"
  34. ASTERISK_FILE_VERSION(__FILE__, "$Revision: 89545 $")
  35. //#include <time.h>
  36. //#include <string.h>
  37. //#include <stdio.h>
  38. //#include <stdlib.h>
  39. //#include <unistd.h>
  40. //#include <errno.h>
  41. #include "asterisk/options.h"
  42. #include "asterisk/logger.h"
  43. #include "asterisk/channel.h"
  44. #include "asterisk/module.h"
  45. #include "asterisk/config.h"
  46. #include "asterisk/file.h"
  47. #include "asterisk/pbx.h"
  48. #include "asterisk/frame.h"
  49. #include "asterisk/utils.h"
  50. #include "asterisk/audiohook.h"
  51. #include "asterisk/manager.h"
  52. /*** DOCUMENTATION
  53. <function name="MUTEAUDIO" language="en_US">
  54. <synopsis>
  55. Muting audio streams in the channel
  56. </synopsis>
  57. <syntax>
  58. <parameter name="direction" required="true">
  59. <para>Must be one of </para>
  60. <enumlist>
  61. <enum name="in">
  62. <para>Inbound stream (to the PBX)</para>
  63. </enum>
  64. <enum name="out">
  65. <para>Outbound stream (from the PBX)</para>
  66. </enum>
  67. <enum name="all">
  68. <para>Both streams</para>
  69. </enum>
  70. </enumlist>
  71. </parameter>
  72. </syntax>
  73. <description>
  74. <para>The MUTEAUDIO function can be used to mute inbound (to the PBX) or outbound audio in a call.
  75. Example:
  76. </para>
  77. <para>
  78. MUTEAUDIO(in)=on
  79. MUTEAUDIO(in)=off
  80. </para>
  81. </description>
  82. </function>
  83. ***/
  84. /*! Our own datastore */
  85. struct mute_information {
  86. struct ast_audiohook audiohook;
  87. int mute_write;
  88. int mute_read;
  89. };
  90. #define TRUE 1
  91. #define FALSE 0
  92. /*! Datastore destroy audiohook callback */
  93. static void destroy_callback(void *data)
  94. {
  95. struct mute_information *mute = data;
  96. /* Destroy the audiohook, and destroy ourselves */
  97. ast_audiohook_destroy(&mute->audiohook);
  98. ast_free(mute);
  99. ast_module_unref(ast_module_info->self);
  100. return;
  101. }
  102. /*! \brief Static structure for datastore information */
  103. static const struct ast_datastore_info mute_datastore = {
  104. .type = "mute",
  105. .destroy = destroy_callback
  106. };
  107. /*! \brief The callback from the audiohook subsystem. We basically get a frame to have fun with */
  108. static int mute_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
  109. {
  110. struct ast_datastore *datastore = NULL;
  111. struct mute_information *mute = NULL;
  112. /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
  113. if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) {
  114. return 0;
  115. }
  116. ast_channel_lock(chan);
  117. /* Grab datastore which contains our mute information */
  118. if (!(datastore = ast_channel_datastore_find(chan, &mute_datastore, NULL))) {
  119. ast_channel_unlock(chan);
  120. ast_debug(2, "Can't find any datastore to use. Bad. \n");
  121. return 0;
  122. }
  123. mute = datastore->data;
  124. /* If this is audio then allow them to increase/decrease the gains */
  125. if (frame->frametype == AST_FRAME_VOICE) {
  126. ast_debug(2, "Audio frame - direction %s mute READ %s WRITE %s\n", direction == AST_AUDIOHOOK_DIRECTION_READ ? "read" : "write", mute->mute_read ? "on" : "off", mute->mute_write ? "on" : "off");
  127. /* Based on direction of frame grab the gain, and confirm it is applicable */
  128. if ((direction == AST_AUDIOHOOK_DIRECTION_READ && mute->mute_read) || (direction == AST_AUDIOHOOK_DIRECTION_WRITE && mute->mute_write)) {
  129. /* Ok, we just want to reset all audio in this frame. Keep NOTHING, thanks. */
  130. ast_frame_clear(frame);
  131. }
  132. }
  133. ast_channel_unlock(chan);
  134. return 0;
  135. }
  136. /*! \brief Initialize mute hook on channel, but don't activate it
  137. \pre Assumes that the channel is locked
  138. */
  139. static struct ast_datastore *initialize_mutehook(struct ast_channel *chan)
  140. {
  141. struct ast_datastore *datastore = NULL;
  142. struct mute_information *mute = NULL;
  143. ast_debug(2, "Initializing new Mute Audiohook \n");
  144. /* Allocate a new datastore to hold the reference to this mute_datastore and audiohook information */
  145. if (!(datastore = ast_datastore_alloc(&mute_datastore, NULL))) {
  146. return NULL;
  147. }
  148. if (!(mute = ast_calloc(1, sizeof(*mute)))) {
  149. ast_datastore_free(datastore);
  150. return NULL;
  151. }
  152. ast_audiohook_init(&mute->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Mute", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
  153. mute->audiohook.manipulate_callback = mute_callback;
  154. datastore->data = mute;
  155. return datastore;
  156. }
  157. /*! \brief Add or activate mute audiohook on channel
  158. Assumes channel is locked
  159. */
  160. static int mute_add_audiohook(struct ast_channel *chan, struct mute_information *mute, struct ast_datastore *datastore)
  161. {
  162. /* Activate the settings */
  163. ast_channel_datastore_add(chan, datastore);
  164. if (ast_audiohook_attach(chan, &mute->audiohook)) {
  165. ast_log(LOG_ERROR, "Failed to attach audiohook for muting channel %s\n", chan->name);
  166. return -1;
  167. }
  168. ast_module_ref(ast_module_info->self);
  169. ast_debug(2, "Initialized audiohook on channel %s\n", chan->name);
  170. return 0;
  171. }
  172. /*! \brief Mute dialplan function */
  173. static int func_mute_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  174. {
  175. struct ast_datastore *datastore = NULL;
  176. struct mute_information *mute = NULL;
  177. int is_new = 0;
  178. ast_channel_lock(chan);
  179. if (!(datastore = ast_channel_datastore_find(chan, &mute_datastore, NULL))) {
  180. if (!(datastore = initialize_mutehook(chan))) {
  181. ast_channel_unlock(chan);
  182. return 0;
  183. }
  184. is_new = 1;
  185. }
  186. mute = datastore->data;
  187. if (!strcasecmp(data, "out")) {
  188. mute->mute_write = ast_true(value);
  189. ast_debug(1, "%s channel - outbound \n", ast_true(value) ? "Muting" : "Unmuting");
  190. } else if (!strcasecmp(data, "in")) {
  191. mute->mute_read = ast_true(value);
  192. ast_debug(1, "%s channel - inbound \n", ast_true(value) ? "Muting" : "Unmuting");
  193. } else if (!strcasecmp(data,"all")) {
  194. mute->mute_write = mute->mute_read = ast_true(value);
  195. }
  196. if (is_new) {
  197. if (mute_add_audiohook(chan, mute, datastore)) {
  198. /* Can't add audiohook - already printed error message */
  199. ast_datastore_free(datastore);
  200. ast_free(mute);
  201. }
  202. }
  203. ast_channel_unlock(chan);
  204. return 0;
  205. }
  206. /* Function for debugging - might be useful */
  207. static struct ast_custom_function mute_function = {
  208. .name = "MUTEAUDIO",
  209. .write = func_mute_write,
  210. };
  211. static int manager_mutestream(struct mansession *s, const struct message *m)
  212. {
  213. const char *channel = astman_get_header(m, "Channel");
  214. const char *id = astman_get_header(m,"ActionID");
  215. const char *state = astman_get_header(m,"State");
  216. const char *direction = astman_get_header(m,"Direction");
  217. char id_text[256] = "";
  218. struct ast_channel *c = NULL;
  219. struct ast_datastore *datastore = NULL;
  220. struct mute_information *mute = NULL;
  221. int is_new = 0;
  222. int turnon = TRUE;
  223. if (ast_strlen_zero(channel)) {
  224. astman_send_error(s, m, "Channel not specified");
  225. return 0;
  226. }
  227. if (ast_strlen_zero(state)) {
  228. astman_send_error(s, m, "State not specified");
  229. return 0;
  230. }
  231. if (ast_strlen_zero(direction)) {
  232. astman_send_error(s, m, "Direction not specified");
  233. return 0;
  234. }
  235. /* Ok, we have everything */
  236. if (!ast_strlen_zero(id)) {
  237. snprintf(id_text, sizeof(id_text), "ActionID: %s\r\n", id);
  238. }
  239. c = ast_channel_get_by_name(channel);
  240. if (!c) {
  241. astman_send_error(s, m, "No such channel");
  242. return 0;
  243. }
  244. ast_channel_lock(c);
  245. if (!(datastore = ast_channel_datastore_find(c, &mute_datastore, NULL))) {
  246. if (!(datastore = initialize_mutehook(c))) {
  247. ast_channel_unlock(c);
  248. ast_channel_unref(c);
  249. return 0;
  250. }
  251. is_new = 1;
  252. }
  253. mute = datastore->data;
  254. turnon = ast_true(state);
  255. if (!strcasecmp(direction, "in")) {
  256. mute->mute_read = turnon;
  257. } else if (!strcasecmp(direction, "out")) {
  258. mute->mute_write = turnon;
  259. } else if (!strcasecmp(direction, "all")) {
  260. mute->mute_read = mute->mute_write = turnon;
  261. }
  262. if (is_new) {
  263. if (mute_add_audiohook(c, mute, datastore)) {
  264. /* Can't add audiohook - already printed error message */
  265. ast_datastore_free(datastore);
  266. ast_free(mute);
  267. }
  268. }
  269. ast_channel_unlock(c);
  270. ast_channel_unref(c);
  271. astman_append(s, "Response: Success\r\n"
  272. "%s"
  273. "\r\n\r\n", id_text);
  274. return 0;
  275. }
  276. static const char mandescr_mutestream[] =
  277. "Description: Mute an incoming or outbound audio stream in a channel.\n"
  278. "Variables: \n"
  279. " Channel: <name> The channel you want to mute.\n"
  280. " Direction: in | out |all The stream you want to mute.\n"
  281. " State: on | off Whether to turn mute on or off.\n"
  282. " ActionID: <id> Optional action ID for this AMI transaction.\n";
  283. static int load_module(void)
  284. {
  285. int res;
  286. res = ast_custom_function_register(&mute_function);
  287. res |= ast_manager_register2("MuteAudio", EVENT_FLAG_SYSTEM, manager_mutestream,
  288. "Mute an audio stream", mandescr_mutestream);
  289. return (res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS);
  290. }
  291. static int unload_module(void)
  292. {
  293. ast_custom_function_unregister(&mute_function);
  294. /* Unregister AMI actions */
  295. ast_manager_unregister("MuteAudio");
  296. return 0;
  297. }
  298. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mute audio stream resources");