Merged revisions 53783 via svnmerge from
[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 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <string.h>
36
37 #include "asterisk/lock.h"
38 #include "asterisk/file.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/module.h"
43
44 static char *app = "Echo";
45
46 static char *synopsis = "Echo audio, video, or DTMF back to the calling party";
47
48 static char *descrip = 
49 "  Echo(): This application will echo any audio, video, or DTMF frames read from\n"
50 "the calling channel back to itself. If the DTMF digit '#' is received, the\n"
51 "application will exit.\n";
52
53
54 static int echo_exec(struct ast_channel *chan, void *data)
55 {
56         int res = -1;
57         int format;
58         struct ast_module_user *u;
59
60         u = ast_module_user_add(chan);
61
62         format = ast_best_codec(chan->nativeformats);
63         ast_set_write_format(chan, format);
64         ast_set_read_format(chan, format);
65
66         while (ast_waitfor(chan, -1) > -1) {
67                 struct ast_frame *f = ast_read(chan);
68                 if (!f)
69                         break;
70                 f->delivery.tv_sec = 0;
71                 f->delivery.tv_usec = 0;
72                 switch (f->frametype) {
73                 case AST_FRAME_DTMF:
74                         if (f->subclass == '#') {
75                                 res = 0;
76                                 if (ast_write(chan, f))
77                                         res = -1;
78                                 ast_frfree(f);
79                                 goto end;
80                         }
81                         /* fall through */
82                 default:
83                         if (ast_write(chan, f)) {
84                                 ast_frfree(f);
85                                 goto end;
86                         }
87                 }
88                 ast_frfree(f);
89         }
90 end:
91         ast_module_user_remove(u);
92         return res;
93 }
94
95 static int unload_module(void)
96 {
97         int res;
98
99         res = ast_unregister_application(app);
100
101         ast_module_user_hangup_all();
102
103         return res;
104 }
105
106 static int load_module(void)
107 {
108         return ast_register_application(app, echo_exec, synopsis, descrip);
109 }
110
111 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Simple Echo Application");