Update application description to be a bit more accurate, and clean up a little bit...
[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  * \ingroup applications
24  */
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <string.h>
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include "asterisk/lock.h"
36 #include "asterisk/file.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/module.h"
41
42 static char *tdesc = "Simple Echo Application";
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 STANDARD_LOCAL_USER;
54
55 LOCAL_USER_DECL;
56
57 static int echo_exec(struct ast_channel *chan, void *data)
58 {
59         int res = -1;
60         int format;
61         struct localuser *u;
62         struct ast_frame *f;
63
64         LOCAL_USER_ADD(u);
65
66         format = ast_best_codec(chan->nativeformats);
67         ast_set_write_format(chan, format);
68         ast_set_read_format(chan, format);
69
70         while (ast_waitfor(chan, -1) > -1) {
71                 f = ast_read(chan);
72                 if (!f)
73                         break;
74                 f->delivery.tv_sec = 0;
75                 f->delivery.tv_usec = 0;
76                 if (f->frametype == AST_FRAME_VOICE) {
77                         if (ast_write(chan, f)) 
78                                 break;
79                 } else if (f->frametype == AST_FRAME_VIDEO) {
80                         if (ast_write(chan, f)) 
81                                 break;
82                 } else if (f->frametype == AST_FRAME_DTMF) {
83                         if (f->subclass == '#') {
84                                 res = 0;
85                                 break;
86                         } else {
87                                 if (ast_write(chan, f))
88                                         break;
89                         }
90                 }
91                 ast_frfree(f);
92         }
93
94         LOCAL_USER_REMOVE(u);
95
96         return res;
97 }
98
99 int unload_module(void)
100 {
101         int res;
102
103         res = ast_unregister_application(app);
104
105         STANDARD_HANGUP_LOCALUSERS;
106
107         return res;
108 }
109
110 int load_module(void)
111 {
112         return ast_register_application(app, echo_exec, synopsis, descrip);
113 }
114
115 char *description(void)
116 {
117         return tdesc;
118 }
119
120 int usecount(void)
121 {
122         int res;
123         STANDARD_USECOUNT(res);
124         return res;
125 }
126
127 char *key()
128 {
129         return ASTERISK_GPL_KEY;
130 }