add the ability to be able to echo DTMF_BEGIN/END, HTML, and IMAGE frames, too
[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 <stdlib.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <string.h>
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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 *tdesc = "Simple Echo Application";
45
46 static char *app = "Echo";
47
48 static char *synopsis = "Echo audio, video, or DTMF back to the calling party";
49
50 static char *descrip = 
51 "  Echo(): This application will echo any audio, video, or DTMF frames read from\n"
52 "the calling channel back to itself. If the DTMF digit '#' is received, the\n"
53 "application will exit.\n";
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
63         LOCAL_USER_ADD(u);
64
65         format = ast_best_codec(chan->nativeformats);
66         ast_set_write_format(chan, format);
67         ast_set_read_format(chan, format);
68
69         while (ast_waitfor(chan, -1) > -1) {
70                 struct ast_frame *f = ast_read(chan);
71                 if (!f)
72                         break;
73                 f->delivery.tv_sec = 0;
74                 f->delivery.tv_usec = 0;
75                 switch (f->frametype) {
76                 case AST_FRAME_DTMF:
77                 case AST_FRAME_DTMF_END:
78                         if (f->subclass == '#') {
79                                 res = 0;
80                                 ast_frfree(f);
81                                 goto end;
82                         }
83                         /* fall through */
84                 case AST_FRAME_DTMF_BEGIN:
85                 case AST_FRAME_VOICE:
86                 case AST_FRAME_VIDEO:
87                 case AST_FRAME_TEXT:
88                 case AST_FRAME_HTML:
89                 case AST_FRAME_IMAGE:
90                         if (ast_write(chan, f)) {
91                                 ast_frfree(f);
92                                 goto end;
93                         }
94                 }
95                 ast_frfree(f);
96         }
97 end:
98         LOCAL_USER_REMOVE(u);
99         return res;
100 }
101
102 static int unload_module(void *mod)
103 {
104         int res;
105
106         res = ast_unregister_application(app);
107
108         STANDARD_HANGUP_LOCALUSERS;
109
110         return res;
111 }
112
113 static int load_module(void *mod)
114 {
115         return ast_register_application(app, echo_exec, synopsis, descrip);
116 }
117
118 static const char *description(void)
119 {
120         return tdesc;
121 }
122
123 static const char *key(void)
124 {
125         return ASTERISK_GPL_KEY;
126 }
127
128 STD_MOD1;