1fbfba5efd7b1eaa552fb5378bde7d5cfb930aa5
[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 <unistd.h>
28 #include <string.h>
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include "asterisk/lock.h"
35 #include "asterisk/file.h"
36 #include "asterisk/logger.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/module.h"
40
41 static char *tdesc = "Simple Echo Application";
42
43 static char *app = "Echo";
44
45 static char *synopsis = "Echo audio read back to the user";
46
47 static char *descrip = 
48 "  Echo():  Echo audio read from channel back to the channel. \n"
49 "User can exit the application by either pressing the '#' key, \n"
50 "or hanging up.\n";
51
52 STANDARD_LOCAL_USER;
53
54 LOCAL_USER_DECL;
55
56 static int echo_exec(struct ast_channel *chan, void *data)
57 {
58         int res=-1;
59         struct localuser *u;
60         struct ast_frame *f;
61         LOCAL_USER_ADD(u);
62         ast_set_write_format(chan, ast_best_codec(chan->nativeformats));
63         ast_set_read_format(chan, ast_best_codec(chan->nativeformats));
64         /* Do our thing here */
65         while(ast_waitfor(chan, -1) > -1) {
66                 f = ast_read(chan);
67                 if (!f)
68                         break;
69                 f->delivery.tv_sec = 0;
70                 f->delivery.tv_usec = 0;
71                 if (f->frametype == AST_FRAME_VOICE) {
72                         if (ast_write(chan, f)) 
73                                 break;
74                 } else if (f->frametype == AST_FRAME_VIDEO) {
75                         if (ast_write(chan, f)) 
76                                 break;
77                 } else if (f->frametype == AST_FRAME_DTMF) {
78                         if (f->subclass == '#') {
79                                 res = 0;
80                                 break;
81                         } else
82                                 if (ast_write(chan, f))
83                                         break;
84                 }
85                 ast_frfree(f);
86         }
87         LOCAL_USER_REMOVE(u);
88         return res;
89 }
90
91 int unload_module(void)
92 {
93         int res;
94
95         res = ast_unregister_application(app);
96
97         STANDARD_HANGUP_LOCALUSERS;
98
99         return res;
100 }
101
102 int load_module(void)
103 {
104         return ast_register_application(app, echo_exec, synopsis, descrip);
105 }
106
107 char *description(void)
108 {
109         return tdesc;
110 }
111
112 int usecount(void)
113 {
114         int res;
115         STANDARD_USECOUNT(res);
116         return res;
117 }
118
119 char *key()
120 {
121         return ASTERISK_GPL_KEY;
122 }