48e6714e211b6d82cf3ed57480181db3ed41eb45
[asterisk/asterisk.git] / apps / app_echo.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@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
19 /*! \file
20  *
21  * \brief Echo application -- play back what you hear to evaluate latency
22  *
23  * \author Mark Spencer <markster@digium.com>
24  *
25  * \ingroup applications
26  */
27
28 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/file.h"
37 #include "asterisk/module.h"
38 #include "asterisk/channel.h"
39
40 /*** DOCUMENTATION
41         <application name="Echo" language="en_US">
42                 <synopsis>
43                         Echo audio, video, DTMF back to the calling party
44                 </synopsis>
45                 <syntax />
46                 <description>
47                         <para>Echos back any audio, video or DTMF frames read from the calling 
48                         channel back to itself. Note: If '#' detected application exits</para>
49                 </description>
50         </application>
51  ***/
52
53 static const char app[] = "Echo";
54
55 static int echo_exec(struct ast_channel *chan, const char *data)
56 {
57         int res = -1;
58         struct ast_format format;
59
60         ast_best_codec(chan->nativeformats, &format);
61         ast_set_write_format(chan, &format);
62         ast_set_read_format(chan, &format);
63
64         while (ast_waitfor(chan, -1) > -1) {
65                 struct ast_frame *f = ast_read(chan);
66                 if (!f) {
67                         break;
68                 }
69                 f->delivery.tv_sec = 0;
70                 f->delivery.tv_usec = 0;
71                 if (ast_write(chan, f)) {
72                         ast_frfree(f);
73                         goto end;
74                 }
75                 if ((f->frametype == AST_FRAME_DTMF) && (f->subclass.integer == '#')) {
76                         res = 0;
77                         ast_frfree(f);
78                         goto end;
79                 }
80                 ast_frfree(f);
81         }
82 end:
83         return res;
84 }
85
86 static int unload_module(void)
87 {
88         return ast_unregister_application(app);
89 }
90
91 static int load_module(void)
92 {
93         return ast_register_application_xml(app, echo_exec);
94 }
95
96 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Simple Echo Application");