Make read/write mode have a lock parameter and use it properly.
[asterisk/asterisk.git] / apps / app_echo.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Echo application -- play back what you hear to evaluate latency
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <asterisk/lock.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/module.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <stdlib.h>
24
25 #include <pthread.h>
26
27
28 static char *tdesc = "Simple Echo Application";
29
30 static char *app = "Echo";
31
32 static char *synopsis = "Echo audio read back to the user";
33
34 static char *descrip = 
35 "  Echo():  Echo audio read from channel back to the channel. Returns 0\n"
36 "if the user exits with the '#' key, or -1 if the user hangs up.\n";
37
38 STANDARD_LOCAL_USER;
39
40 LOCAL_USER_DECL;
41
42 static int echo_exec(struct ast_channel *chan, void *data)
43 {
44         int res=-1;
45         struct localuser *u;
46         struct ast_frame *f;
47         LOCAL_USER_ADD(u);
48         ast_set_write_format(chan, ast_best_codec(chan->nativeformats), 1);
49         ast_set_read_format(chan, ast_best_codec(chan->nativeformats), 1);
50         /* Do our thing here */
51         while(ast_waitfor(chan, -1) > -1) {
52                 f = ast_read(chan);
53                 if (!f)
54                         break;
55                 f->delivery.tv_sec = 0;
56                 f->delivery.tv_usec = 0;
57                 if (f->frametype == AST_FRAME_VOICE) {
58                         if (ast_write(chan, f)) 
59                                 break;
60                 } else if (f->frametype == AST_FRAME_VIDEO) {
61                         if (ast_write(chan, f)) 
62                                 break;
63                 } else if (f->frametype == AST_FRAME_DTMF) {
64                         if (f->subclass == '#') {
65                                 res = 0;
66                                 break;
67                         } else
68                                 if (ast_write(chan, f))
69                                         break;
70                 }
71                 ast_frfree(f);
72         }
73         LOCAL_USER_REMOVE(u);
74         return res;
75 }
76
77 int unload_module(void)
78 {
79         STANDARD_HANGUP_LOCALUSERS;
80         return ast_unregister_application(app);
81 }
82
83 int load_module(void)
84 {
85         return ast_register_application(app, echo_exec, synopsis, descrip);
86 }
87
88 char *description(void)
89 {
90         return tdesc;
91 }
92
93 int usecount(void)
94 {
95         int res;
96         STANDARD_USECOUNT(res);
97         return res;
98 }
99
100 char *key()
101 {
102         return ASTERISK_GPL_KEY;
103 }