summaryrefslogtreecommitdiff
path: root/apps/app_stream_echo.c
blob: 717ed1c8860ea7edffc64846b6efe7ee3fb7fdd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2017, Digium, Inc.
 *
 * Kevin Harwell <kharwell@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief Stream echo application
 *
 * \author Kevin Harwell <kharwell@digium.com>
 */

/*** MODULEINFO
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#include "asterisk/app.h"
#include "asterisk/conversions.h"
#include "asterisk/file.h"
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/stream.h"

/*** DOCUMENTATION
	<application name="StreamEcho" language="en_US">
		<synopsis>
			Echo media, up to 'N' streams of a type, and DTMF back to the calling party
		</synopsis>
		<syntax>
		<parameter name="num" required="false">
			<para>The number of streams of a type to echo back. If '0' is specified then
			all streams of a type are removed.</para>
		</parameter>
		<parameter name="type" required="false">
			<para>The media type of the stream(s) to add or remove (in the case of "num"
			being '0'). This can be set to either "audio" or "video" (default). If "num"
			is empty (i.e. not specified) then this parameter is ignored.</para>
		</parameter>
		</syntax>
		<description>
			<para>If a "num" (the number of streams) is not given then this simply echos
			back any media or DTMF frames (note, however if '#' is detected then the
			application exits) read from the calling channel back to itself. This means
			for any relevant frame read from a particular stream it is written back out
			to the associated write stream in a one to one fashion.
			</para>
			<para>However if a "num" is specified, and if the calling channel allows it
			(a new offer is made requesting the allowance of additional streams) then any
			any media received, like before, is echoed back onto each stream. However, in
			this case a relevant frame received on a stream of the given "type" is also
			echoed back out to the other streams of that same type. It should be noted that
			when operating in this mode only the first stream found of the given "type" is
			allowed from the original offer. And this first stream found is also the only
			stream of that "type" granted read (send/receive) capabilities in the new offer
			whereas the additional ones are set to receive only.</para>
			<note><para>This does not echo CONTROL, MODEM, or NULL frames.</para></note>
		</description>
	</application>
 ***/

static const char app[] = "StreamEcho";

static int stream_echo_write_error(struct ast_channel *chan, struct ast_frame *frame, int pos)
{
	char frame_type[32];
	const char *media_type;
	struct ast_stream *stream;

	ast_frame_type2str(frame->frametype, frame_type, sizeof(frame_type));

	stream = pos < 0 ?
		ast_channel_get_default_stream(chan, ast_format_get_type(frame->subclass.format)) :
		ast_stream_topology_get_stream(ast_channel_get_stream_topology(chan), pos);

	media_type = ast_codec_media_type2str(ast_stream_get_type(stream));

	ast_log(LOG_ERROR, "%s - unable to write frame type '%s' to stream type '%s' at "
		"position '%d'\n", ast_channel_name(chan), frame_type, media_type,
		ast_stream_get_position(stream));

	return -1;
}

static int stream_echo_write(struct ast_channel *chan, struct ast_frame *frame,
	enum ast_media_type type, int one_to_one)
{
	int i;
	int num;
	struct ast_stream_topology *topology;

	/*
	 * Since this is an echo application if we get a frame in on a stream
	 * we simply want to echo it back out onto the same stream number.
	 */
	num = ast_channel_is_multistream(chan) ? frame->stream_num : -1;
	if (ast_write_stream(chan, num, frame)) {
		return stream_echo_write_error(chan, frame, num);
	}

	/*
	 * If the frame's type and given type don't match, or we are operating in
	 * a one to one stream echo mode then there is nothing left to do.
	 *
	 * Note, if the channel is not multi-stream capable then one_to_one will
	 * always be true, so it is safe to also not check for that here too.
	 */
	if (one_to_one || !frame->subclass.format ||
	    ast_format_get_type(frame->subclass.format) != type) {
		return 0;
	}

	/*
	 * However, if we are operating in a single stream echoed to many stream
	 * mode, and the frame's type matches the given type then we also need to
	 * find the other streams of the same type and write out to those streams
	 * as well.
	 *
	 * If we are here, then it's accepted that whatever stream number the frame
	 * was read from for the given type is the only one set to send/receive,
	 * while the others of the same type are set to receive only. Since we
	 * shouldn't assume any order to the streams, we'll loop back through all
	 * streams in the channel's topology writing only to those of the same type.
	 * And, of course also not the stream which has already been written to.
	 */
	topology = ast_channel_get_stream_topology(chan);

	for (i = 0; i < ast_stream_topology_get_count(topology); ++i) {
		struct ast_stream *stream = ast_stream_topology_get_stream(topology, i);
		if (num != i && ast_stream_get_type(stream) == type) {
			if (ast_write_stream(chan, i, frame)) {
				return stream_echo_write_error(chan, frame, i);
			}
		}
	}

	return 0;
}

static int stream_echo_perform(struct ast_channel *chan,
	struct ast_stream_topology *topology, enum ast_media_type type)
{
	int update_sent = 0;
	int request_change = topology != NULL;
	int one_to_one = 1;

	while (ast_waitfor(chan, -1) > -1) {
		struct ast_frame *f;

		if (request_change) {
			/* Request a change to the new topology */
			if (ast_channel_request_stream_topology_change(chan, topology, NULL)) {
				ast_log(LOG_WARNING, "Request stream topology change not supported "
					"by channel '%s'\n", ast_channel_name(chan));
			}
			request_change = 0;
		}

		f = ast_read_stream(chan);
		if (!f) {
			return -1;
		}

		if ((f->frametype == AST_FRAME_DTMF) && (f->subclass.integer == '#')) {
			ast_frfree(f);
			break;
		}

		f->delivery.tv_sec = 0;
		f->delivery.tv_usec = 0;

		if (f->frametype == AST_FRAME_CONTROL) {
			if (f->subclass.integer == AST_CONTROL_VIDUPDATE && !update_sent) {
				if (stream_echo_write(chan, f, type, one_to_one)) {
					ast_frfree(f);
					return -1;
				}
				update_sent = 1;
			} else if (f->subclass.integer == AST_CONTROL_SRCCHANGE) {
				update_sent = 0;
			} else if (f->subclass.integer == AST_CONTROL_STREAM_TOPOLOGY_CHANGED) {
				update_sent = 0;
				one_to_one = 0; /* Switch writing to one to many */
			}
		} else if (f->frametype == AST_FRAME_VIDEO && !update_sent){
			struct ast_frame frame = {
				.frametype = AST_FRAME_CONTROL,
				.subclass.integer = AST_CONTROL_VIDUPDATE,
			};
			stream_echo_write(chan, &frame, type, one_to_one);
			update_sent = 1;
		}

		if (f->frametype != AST_FRAME_CONTROL &&
		    f->frametype != AST_FRAME_MODEM &&
		    f->frametype != AST_FRAME_NULL &&
		    stream_echo_write(chan, f, type, one_to_one)) {
			ast_frfree(f);
			return -1;
		}

		ast_frfree(f);
	}

	return 0;
}

static struct ast_stream_topology *stream_echo_topology_alloc(
	struct ast_stream_topology *original, unsigned int num, enum ast_media_type type)
{
	int i, n = num;
	struct ast_stream_topology *res = ast_stream_topology_alloc();

	if (!res) {
		return NULL;
	}

	/*
	 * Clone every stream of a type not matching the given one. If the type
	 * matches clone only the first stream found for the given type. Then for
	 * that stream clone it again up to num - 1 times. Ignore any other streams
	 * of the same matched type in the original topology.
	 *
	 * So for instance if the original stream contains 1 audio stream and 2 video
	 * streams (video stream 'A' and video stream 'B'), num is '3', and the given
	 * type is 'video' then the resulting topology will contain a clone of the
	 * audio stream along with 3 clones of video stream 'A'. Video stream 'B' is
	 * not copied over.
	 */
	for (i = 0; i < ast_stream_topology_get_count(original); ++i) {
		struct ast_stream *stream = ast_stream_topology_get_stream(original, i);

		if (!n && ast_stream_get_type(stream) == type) {
			/* Don't copy any[more] of the given type */
			continue;
		}

		if (ast_stream_get_state(stream) == AST_STREAM_STATE_REMOVED) {
			/* Don't copy removed/declined streams */
			continue;
		}

		do {
			stream = ast_stream_clone(stream, NULL);

			if (!stream || ast_stream_topology_append_stream(res, stream) < 0) {
				ast_stream_free(stream);
				ast_stream_topology_free(res);
				return NULL;
			}

			if (ast_stream_get_type(stream) != type) {
				/* Do not multiply non matching streams */
				break;
			}

			/*
			 * Since num is not zero yet (i.e. this is first stream found to
			 * match on the type) and the types match then loop num - 1 times
			 * cloning the same stream.
			 */
			ast_stream_set_state(stream, n == num ?
			     AST_STREAM_STATE_SENDRECV : AST_STREAM_STATE_RECVONLY);
		} while (--n);
	}

	return res;
}

static int stream_echo_exec(struct ast_channel *chan, const char *data)
{
	int res;
	unsigned int num = 0;
	enum ast_media_type type;
	char *parse;
	struct ast_stream_topology *topology;

	AST_DECLARE_APP_ARGS(args,
		AST_APP_ARG(num);
		AST_APP_ARG(type);
	);

	parse = ast_strdupa(data);
	AST_STANDARD_APP_ARGS(args, parse);

	if (ast_strlen_zero(args.num)) {
		/*
		 * If a number is not given then no topology is to be created
		 * and renegotiated. The app will just echo back each stream
		 * received to itself.
		 */
		return stream_echo_perform(chan, NULL, AST_MEDIA_TYPE_UNKNOWN);
	}

	if (ast_str_to_uint(args.num, &num)) {
		ast_log(LOG_ERROR, "Failed to parse the first parameter '%s' into a"
			" greater than or equal to zero\n", args.num);
		return -1;
	}

	type = ast_strlen_zero(args.type) ? AST_MEDIA_TYPE_VIDEO :
		ast_media_type_from_str(args.type);

	topology = stream_echo_topology_alloc(
		ast_channel_get_stream_topology(chan), num, type);
	if (!topology) {
		ast_log(LOG_ERROR, "Unable to create '%u' streams of type '%s' to"
			" the topology\n", num, ast_codec_media_type2str(type));
		return -1;
	}

	res = stream_echo_perform(chan, topology, type);

	if (ast_channel_get_stream_topology(chan) != topology) {
		ast_stream_topology_free(topology);
	}

	return res;
}

static int unload_module(void)
{
	return ast_unregister_application(app);
}

static int load_module(void)
{
	return ast_register_application_xml(app, stream_echo_exec);
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stream Echo Application");