bug #8076 check option_debug before printing to debug channel.
[asterisk/asterisk.git] / apps / app_ices.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 Stream to an icecast server via ICES (see contrib/asterisk-ices.xml)
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 <string.h>
33 #include <stdio.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <sys/time.h>
39 #include <errno.h>
40
41 #include "asterisk/lock.h"
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/frame.h"
46 #include "asterisk/pbx.h"
47 #include "asterisk/module.h"
48 #include "asterisk/translate.h"
49 #include "asterisk/options.h"
50
51 #define ICES "/usr/bin/ices"
52 #define LOCAL_ICES "/usr/local/bin/ices"
53
54 static char *app = "ICES";
55
56 static char *synopsis = "Encode and stream using 'ices'";
57
58 static char *descrip = 
59 "  ICES(config.xml) Streams to an icecast server using ices\n"
60 "(available separately).  A configuration file must be supplied\n"
61 "for ices (see examples/asterisk-ices.conf). \n";
62
63
64 static int icesencode(char *filename, int fd)
65 {
66         int res;
67         int x;
68         res = fork();
69         if (res < 0) 
70                 ast_log(LOG_WARNING, "Fork failed\n");
71         if (res)
72                 return res;
73         if (ast_opt_high_priority)
74                 ast_set_priority(0);
75         dup2(fd, STDIN_FILENO);
76         for (x=STDERR_FILENO + 1;x<256;x++) {
77                 if ((x != STDIN_FILENO) && (x != STDOUT_FILENO))
78                         close(x);
79         }
80         /* Most commonly installed in /usr/local/bin */
81         execl(ICES, "ices", filename, (char *)NULL);
82         /* But many places has it in /usr/bin */
83         execl(LOCAL_ICES, "ices", filename, (char *)NULL);
84         /* As a last-ditch effort, try to use PATH */
85         execlp("ices", "ices", filename, (char *)NULL);
86         ast_log(LOG_WARNING, "Execute of ices failed\n");
87         return -1;
88 }
89
90 static int ices_exec(struct ast_channel *chan, void *data)
91 {
92         int res=0;
93         struct ast_module_user *u;
94         int fds[2];
95         int ms = -1;
96         int pid = -1;
97         int flags;
98         int oreadformat;
99         struct timeval last;
100         struct ast_frame *f;
101         char filename[256]="";
102         char *c;
103
104         if (ast_strlen_zero(data)) {
105                 ast_log(LOG_WARNING, "ICES requires an argument (configfile.xml)\n");
106                 return -1;
107         }
108
109         u = ast_module_user_add(chan);
110         
111         last = ast_tv(0, 0);
112         
113         if (pipe(fds)) {
114                 ast_log(LOG_WARNING, "Unable to create pipe\n");
115                 ast_module_user_remove(u);
116                 return -1;
117         }
118         flags = fcntl(fds[1], F_GETFL);
119         fcntl(fds[1], F_SETFL, flags | O_NONBLOCK);
120         
121         ast_stopstream(chan);
122
123         if (chan->_state != AST_STATE_UP)
124                 res = ast_answer(chan);
125                 
126         if (res) {
127                 close(fds[0]);
128                 close(fds[1]);
129                 ast_log(LOG_WARNING, "Answer failed!\n");
130                 ast_module_user_remove(u);
131                 return -1;
132         }
133
134         oreadformat = chan->readformat;
135         res = ast_set_read_format(chan, AST_FORMAT_SLINEAR);
136         if (res < 0) {
137                 close(fds[0]);
138                 close(fds[1]);
139                 ast_log(LOG_WARNING, "Unable to set write format to signed linear\n");
140                 ast_module_user_remove(u);
141                 return -1;
142         }
143         if (((char *)data)[0] == '/')
144                 strncpy(filename, (char *)data, sizeof(filename) - 1);
145         else
146                 snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, (char *)data);
147         /* Placeholder for options */           
148         c = strchr(filename, '|');
149         if (c)
150                 *c = '\0';      
151         res = icesencode(filename, fds[0]);
152         close(fds[0]);
153         if (res >= 0) {
154                 pid = res;
155                 for (;;) {
156                         /* Wait for audio, and stream */
157                         ms = ast_waitfor(chan, -1);
158                         if (ms < 0) {
159                                 if (option_debug)
160                                         ast_log(LOG_DEBUG, "Hangup detected\n");
161                                 res = -1;
162                                 break;
163                         }
164                         f = ast_read(chan);
165                         if (!f) {
166                                 if (option_debug)
167                                         ast_log(LOG_DEBUG, "Null frame == hangup() detected\n");
168                                 res = -1;
169                                 break;
170                         }
171                         if (f->frametype == AST_FRAME_VOICE) {
172                                 res = write(fds[1], f->data, f->datalen);
173                                 if (res < 0) {
174                                         if (errno != EAGAIN) {
175                                                 ast_log(LOG_WARNING, "Write failed to pipe: %s\n", strerror(errno));
176                                                 res = -1;
177                                                 ast_frfree(f);
178                                                 break;
179                                         }
180                                 }
181                         }
182                         ast_frfree(f);
183                 }
184         }
185         close(fds[1]);
186         
187         if (pid > -1)
188                 kill(pid, SIGKILL);
189         if (!res && oreadformat)
190                 ast_set_read_format(chan, oreadformat);
191
192         ast_module_user_remove(u);
193
194         return res;
195 }
196
197 static int unload_module(void)
198 {
199         int res;
200
201         res = ast_unregister_application(app);
202
203         ast_module_user_hangup_all();
204
205         return res;
206 }
207
208 static int load_module(void)
209 {
210         return ast_register_application(app, ices_exec, synopsis, descrip);
211 }
212
213 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Encode and Stream via icecast and ices");