Debian Codegraph @

res/res_mutestream.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2009, Olle E. Johansson
5 *
6 * Olle E. Johansson <[email protected]>
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
19 /*! \file
20 *
21 * \brief MUTESTREAM audiohooks
22 *
23 * \author Olle E. Johansson <[email protected]>
24 *
25 * \ingroup functions
26 *
27 * \note This module only handles audio streams today, but can easily be appended to also
28 * zero out text streams if there's an application for it.
29 * When we know and understand what happens if we zero out video, we can do that too.
30 */
31
32 /*** MODULEINFO
33 <support_level>core</support_level>
34 ***/
35
36 #include "asterisk.h"
37
38 #include "asterisk/options.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/module.h"
42 #include "asterisk/config.h"
43 #include "asterisk/file.h"
44 #include "asterisk/pbx.h"
45 #include "asterisk/frame.h"
46 #include "asterisk/utils.h"
47 #include "asterisk/audiohook.h"
48 #include "asterisk/manager.h"
49
50 /*** DOCUMENTATION
51 <function name="MUTEAUDIO" language="en_US">
52 <since>
53 <version>1.8.0</version>
54 </since>
55 <synopsis>
56 Muting audio streams in the channel
57 </synopsis>
58 <syntax>
59 <parameter name="direction" required="true">
60 <para>Must be one of </para>
61 <enumlist>
62 <enum name="in">
63 <para>Inbound stream (to the PBX)</para>
64 </enum>
65 <enum name="out">
66 <para>Outbound stream (from the PBX)</para>
67 </enum>
68 <enum name="all">
69 <para>Both streams</para>
70 </enum>
71 </enumlist>
72 </parameter>
73 </syntax>
74 <description>
75 <para>The MUTEAUDIO function can be used to mute inbound (to the PBX) or outbound audio in a call.</para>
76 <example title="Mute incoming audio">
77 exten => s,1,Set(MUTEAUDIO(in)=on)
78 </example>
79 <example title="Do not mute incoming audio">
80 exten => s,1,Set(MUTEAUDIO(in)=off)
81 </example>
82 </description>
83 </function>
84 <manager name="MuteAudio" language="en_US">
85 <since>
86 <version>1.8.0</version>
87 </since>
88 <synopsis>
89 Mute an audio stream.
90 </synopsis>
91 <syntax>
92 <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
93 <parameter name="Channel" required="true">
94 <para>The channel you want to mute.</para>
95 </parameter>
96 <parameter name="Direction" required="true">
97 <enumlist>
98 <enum name="in">
99 <para>Set muting on inbound audio stream. (to the PBX)</para>
100 </enum>
101 <enum name="out">
102 <para>Set muting on outbound audio stream. (from the PBX)</para>
103 </enum>
104 <enum name="all">
105 <para>Set muting on inbound and outbound audio streams.</para>
106 </enum>
107 </enumlist>
108 </parameter>
109 <parameter name="State" required="true">
110 <enumlist>
111 <enum name="on">
112 <para>Turn muting on.</para>
113 </enum>
114 <enum name="off">
115 <para>Turn muting off.</para>
116 </enum>
117 </enumlist>
118 </parameter>
119 </syntax>
120 <description>
121 <para>Mute an incoming or outgoing audio stream on a channel.</para>
122 </description>
123 </manager>
124 ***/
125
126
127 static int mute_channel(struct ast_channel *chan, const char *direction, int mute)
128 {
129 unsigned int mute_direction = 0;
130 enum ast_frame_type frametype = AST_FRAME_VOICE;
131 int ret = 0;
132
133 if (!strcmp(direction, "in")) {
134 mute_direction = AST_MUTE_DIRECTION_READ;
135 } else if (!strcmp(direction, "out")) {
136 mute_direction = AST_MUTE_DIRECTION_WRITE;
137 } else if (!strcmp(direction, "all")) {
138 mute_direction = AST_MUTE_DIRECTION_READ | AST_MUTE_DIRECTION_WRITE;
139 } else {
140 return -1;
141 }
142
143 ast_channel_lock(chan);
144
145 if (mute) {
146 ret = ast_channel_suppress(chan, mute_direction, frametype);
147 } else {
148 ret = ast_channel_unsuppress(chan, mute_direction, frametype);
149 }
150
151 ast_channel_unlock(chan);
152
153 return ret;
154 }
155
156 /*! \brief Mute dialplan function */
157 static int func_mute_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
158 {
159 if (!chan) {
160 ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
161 return -1;
162 }
163
164 return mute_channel(chan, data, ast_true(value));
165 }
166
167 /* Function for debugging - might be useful */
168 static struct ast_custom_function mute_function = {
169 .name = "MUTEAUDIO",
170 .write = func_mute_write,
171 };
172
173 static int manager_mutestream(struct mansession *s, const struct message *m)
174 {
175 const char *channel = astman_get_header(m, "Channel");
176 const char *id = astman_get_header(m,"ActionID");
177 const char *state = astman_get_header(m,"State");
178 const char *direction = astman_get_header(m,"Direction");
179 char id_text[256];
180 struct ast_channel *c = NULL;
181
182 if (ast_strlen_zero(channel)) {
183 astman_send_error(s, m, "Channel not specified");
184 return 0;
185 }
186 if (ast_strlen_zero(state)) {
187 astman_send_error(s, m, "State not specified");
188 return 0;
189 }
190 if (ast_strlen_zero(direction)) {
191 astman_send_error(s, m, "Direction not specified");
192 return 0;
193 }
194 /* Ok, we have everything */
195
196 c = ast_channel_get_by_name(channel);
197 if (!c) {
198 astman_send_error(s, m, "No such channel");
199 return 0;
200 }
201
202 if (mute_channel(c, direction, ast_true(state))) {
203 astman_send_error(s, m, "Failed to mute/unmute stream");
204 ast_channel_unref(c);
205 return 0;
206 }
207
208 ast_channel_unref(c);
209
210 if (!ast_strlen_zero(id)) {
211 snprintf(id_text, sizeof(id_text), "ActionID: %s\r\n", id);
212 } else {
213 id_text[0] = '\0';
214 }
215 astman_append(s, "Response: Success\r\n"
216 "%s"
217 "\r\n", id_text);
218 return 0;
219 }
220
221
222 static int load_module(void)
223 {
224 int res;
225
226 res = ast_custom_function_register(&mute_function);
227 res |= ast_manager_register_xml("MuteAudio", EVENT_FLAG_SYSTEM, manager_mutestream);
228
229 return (res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS);
230 }
231
232 static int unload_module(void)
233 {
234 ast_custom_function_unregister(&mute_function);
235 /* Unregister AMI actions */
236 ast_manager_unregister("MuteAudio");
237
238 return 0;
239 }
240
241 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mute audio stream resources");