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