Debian Codegraph @

apps/app_stream_echo.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2017, Digium, Inc.
5 *
6 * Kevin Harwell <[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 Stream echo application
22 *
23 * \author Kevin Harwell <[email protected]>
24 */
25
26 /*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30 #include "asterisk.h"
31
32 #include "asterisk/app.h"
33 #include "asterisk/conversions.h"
34 #include "asterisk/file.h"
35 #include "asterisk/module.h"
36 #include "asterisk/channel.h"
37 #include "asterisk/stream.h"
38
39 /*** DOCUMENTATION
40 <application name="StreamEcho" language="en_US">
41 <since>
42 <version>15.0.0</version>
43 </since>
44 <synopsis>
45 Echo media, up to 'N' streams of a type, and DTMF back to the calling party
46 </synopsis>
47 <syntax>
48 <parameter name="num" required="false">
49 <para>The number of streams of a type to echo back. If '0' is specified then
50 all streams of a type are removed.</para>
51 </parameter>
52 <parameter name="type" required="false">
53 <para>The media type of the stream(s) to add or remove (in the case of "num"
54 being '0'). This can be set to either "audio" or "video" (default). If "num"
55 is empty (i.e. not specified) then this parameter is ignored.</para>
56 </parameter>
57 </syntax>
58 <description>
59 <para>If a "num" (the number of streams) is not given then this simply echos
60 back any media or DTMF frames (note, however if '#' is detected then the
61 application exits) read from the calling channel back to itself. This means
62 for any relevant frame read from a particular stream it is written back out
63 to the associated write stream in a one to one fashion.
64 </para>
65 <para>However if a "num" is specified, and if the calling channel allows it
66 (a new offer is made requesting the allowance of additional streams) then any
67 any media received, like before, is echoed back onto each stream. However, in
68 this case a relevant frame received on a stream of the given "type" is also
69 echoed back out to the other streams of that same type. It should be noted that
70 when operating in this mode only the first stream found of the given "type" is
71 allowed from the original offer. And this first stream found is also the only
72 stream of that "type" granted read (send/receive) capabilities in the new offer
73 whereas the additional ones are set to receive only.</para>
74 <note><para>This does not echo CONTROL, MODEM, or NULL frames.</para></note>
75 </description>
76 </application>
77 ***/
78
79 static const char app[] = "StreamEcho";
80
81 static int stream_echo_write_error(struct ast_channel *chan, int stream_num, struct ast_frame *frame)
82 {
83 char frame_type[32];
84 const char *media_type;
85 struct ast_stream *stream;
86
87 ast_frame_type2str(frame->frametype, frame_type, sizeof(frame_type));
88
89 stream = stream_num < 0 ?
90 ast_channel_get_default_stream(chan, ast_format_get_type(frame->subclass.format)) :
91 ast_stream_topology_get_stream(ast_channel_get_stream_topology(chan), stream_num);
92
93 if (!stream) {
94 return -1;
95 }
96
97 media_type = ast_codec_media_type2str(ast_stream_get_type(stream));
98
99 ast_log(LOG_ERROR, "%s - unable to write frame type '%s' to stream type '%s' at "
100 "position '%d'\n", ast_channel_name(chan), frame_type, media_type,
101 ast_stream_get_position(stream));
102
103 return -1;
104 }
105
106 static int stream_echo_write(struct ast_channel *chan, struct ast_frame *frame,
107 enum ast_media_type type, int one_to_one)
108 {
109 int i;
110 int num;
111 struct ast_stream_topology *topology;
112
113 /*
114 * Since this is an echo application if we get a frame in on a stream
115 * we simply want to echo it back out onto the same stream number.
116 */
117 num = ast_channel_is_multistream(chan) ? frame->stream_num : -1;
118 if (ast_write_stream(chan, num, frame)) {
119 return stream_echo_write_error(chan, num, frame);
120 }
121
122 /*
123 * If the frame's type and given type don't match, or we are operating in
124 * a one to one stream echo mode then there is nothing left to do.
125 *
126 * Note, if the channel is not multi-stream capable then one_to_one will
127 * always be true, so it is safe to also not check for that here too.
128 */
129 if (one_to_one || !frame->subclass.format ||
130 ast_format_get_type(frame->subclass.format) != type) {
131 return 0;
132 }
133
134 /*
135 * However, if we are operating in a single stream echoed to many stream
136 * mode, and the frame's type matches the given type then we also need to
137 * find the other streams of the same type and write out to those streams
138 * as well.
139 *
140 * If we are here, then it's accepted that whatever stream number the frame
141 * was read from for the given type is the only one set to send/receive,
142 * while the others of the same type are set to receive only. Since we
143 * shouldn't assume any order to the streams, we'll loop back through all
144 * streams in the channel's topology writing only to those of the same type.
145 * And, of course also not the stream which has already been written to.
146 */
147 topology = ast_channel_get_stream_topology(chan);
148
149 for (i = 0; i < ast_stream_topology_get_count(topology); ++i) {
150 struct ast_stream *stream = ast_stream_topology_get_stream(topology, i);
151 if (num != i && ast_stream_get_type(stream) == type) {
152 if (ast_write_stream(chan, i, frame)) {
153 return stream_echo_write_error(chan, i, frame);
154 }
155 }
156 }
157
158 return 0;
159 }
160
161 static int stream_echo_perform(struct ast_channel *chan,
162 struct ast_stream_topology *topology, enum ast_media_type type)
163 {
164 int update_sent = 0;
165 int request_change = topology != NULL;
166 int one_to_one = 1;
167
168 while (ast_waitfor(chan, -1) > -1) {
169 struct ast_frame *f;
170
171 if (request_change) {
172 /* Request a change to the new topology */
173 if (ast_channel_request_stream_topology_change(chan, topology, NULL)) {
174 ast_log(LOG_WARNING, "Request stream topology change not supported "
175 "by channel '%s'\n", ast_channel_name(chan));
176 }
177 request_change = 0;
178 }
179
180 f = ast_read_stream(chan);
181 if (!f) {
182 return -1;
183 }
184
185 if ((f->frametype == AST_FRAME_DTMF) && (f->subclass.integer == '#')) {
186 ast_frfree(f);
187 break;
188 }
189
190 f->delivery.tv_sec = 0;
191 f->delivery.tv_usec = 0;
192
193 if (f->frametype == AST_FRAME_CONTROL) {
194 if (f->subclass.integer == AST_CONTROL_VIDUPDATE && !update_sent) {
195 if (stream_echo_write(chan, f, type, one_to_one)) {
196 ast_frfree(f);
197 return -1;
198 }
199 update_sent = 1;
200 } else if (f->subclass.integer == AST_CONTROL_SRCCHANGE) {
201 update_sent = 0;
202 } else if (f->subclass.integer == AST_CONTROL_STREAM_TOPOLOGY_CHANGED) {
203 update_sent = 0;
204 one_to_one = 0; /* Switch writing to one to many */
205 }
206 } else if (f->frametype == AST_FRAME_VIDEO && !update_sent){
207 struct ast_frame frame = {
208 .frametype = AST_FRAME_CONTROL,
209 .subclass.integer = AST_CONTROL_VIDUPDATE,
210 };
211 stream_echo_write(chan, &frame, type, one_to_one);
212 update_sent = 1;
213 }
214
215 if (f->frametype != AST_FRAME_CONTROL &&
216 f->frametype != AST_FRAME_MODEM &&
217 f->frametype != AST_FRAME_NULL &&
218 stream_echo_write(chan, f, type, one_to_one)) {
219 ast_frfree(f);
220 return -1;
221 }
222
223 ast_frfree(f);
224 }
225
226 return 0;
227 }
228
229 static struct ast_stream_topology *stream_echo_topology_alloc(
230 struct ast_stream_topology *original, unsigned int num, enum ast_media_type type)
231 {
232 int i, n = num;
233 struct ast_stream_topology *res = ast_stream_topology_alloc();
234
235 if (!res) {
236 return NULL;
237 }
238
239 /*
240 * Clone every stream of a type not matching the given one. If the type
241 * matches clone only the first stream found for the given type. Then for
242 * that stream clone it again up to num - 1 times. Ignore any other streams
243 * of the same matched type in the original topology.
244 *
245 * So for instance if the original stream contains 1 audio stream and 2 video
246 * streams (video stream 'A' and video stream 'B'), num is '3', and the given
247 * type is 'video' then the resulting topology will contain a clone of the
248 * audio stream along with 3 clones of video stream 'A'. Video stream 'B' is
249 * not copied over.
250 */
251 for (i = 0; i < ast_stream_topology_get_count(original); ++i) {
252 struct ast_stream *stream = ast_stream_topology_get_stream(original, i);
253
254 if (!n && ast_stream_get_type(stream) == type) {
255 /* Don't copy any[more] of the given type */
256 continue;
257 }
258
259 if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED) {
260 /* Don't copy removed/declined streams */
261 continue;
262 }
263
264 do {
265 stream = ast_stream_clone(stream, NULL);
266
267 if (!stream || ast_stream_topology_append_stream(res, stream) < 0) {
268 ast_stream_free(stream);
269 ast_stream_topology_free(res);
270 return NULL;
271 }
272
273 if (ast_stream_get_type(stream) != type) {
274 /* Do not multiply non matching streams */
275 break;
276 }
277
278 /*
279 * Since num is not zero yet (i.e. this is first stream found to
280 * match on the type) and the types match then loop num - 1 times
281 * cloning the same stream.
282 */
283 ast_stream_set_state(stream, n == num ?
284 AST_STREAM_STATE_SENDRECV : AST_STREAM_STATE_SENDONLY);
285 } while (--n);
286 }
287
288 return res;
289 }
290
291 static int stream_echo_exec(struct ast_channel *chan, const char *data)
292 {
293 int res;
294 unsigned int num = 0;
295 enum ast_media_type type;
296 char *parse;
297 struct ast_stream_topology *topology;
298
299 AST_DECLARE_APP_ARGS(args,
300 AST_APP_ARG(num);
301 AST_APP_ARG(type);
302 );
303
304 parse = ast_strdupa(data);
305 AST_STANDARD_APP_ARGS(args, parse);
306
307 if (ast_strlen_zero(args.num)) {
308 /*
309 * If a number is not given then no topology is to be created
310 * and renegotiated. The app will just echo back each stream
311 * received to itself.
312 */
313 return stream_echo_perform(chan, NULL, AST_MEDIA_TYPE_UNKNOWN);
314 }
315
316 if (ast_str_to_uint(args.num, &num)) {
317 ast_log(LOG_ERROR, "Failed to parse the first parameter '%s' into a"
318 " greater than or equal to zero\n", args.num);
319 return -1;
320 }
321
322 type = ast_strlen_zero(args.type) ? AST_MEDIA_TYPE_VIDEO :
323 ast_media_type_from_str(args.type);
324
325 topology = stream_echo_topology_alloc(
326 ast_channel_get_stream_topology(chan), num, type);
327 if (!topology) {
328 ast_log(LOG_ERROR, "Unable to create '%u' streams of type '%s' to"
329 " the topology\n", num, ast_codec_media_type2str(type));
330 return -1;
331 }
332
333 res = stream_echo_perform(chan, topology, type);
334
335 if (ast_channel_get_stream_topology(chan) != topology) {
336 ast_stream_topology_free(topology);
337 }
338
339 return res;
340 }
341
342 static int unload_module(void)
343 {
344 return ast_unregister_application(app);
345 }
346
347 static int load_module(void)
348 {
349 return ast_register_application_xml(app, stream_echo_exec);
350 }
351
352 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stream Echo Application");