]> git.tuebingen.mpg.de Git - paraslash.git/blob - write.c
check_wav: return error on premature end of file.
[paraslash.git] / write.c
1 /*
2  * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 #include "para.h"
20 #include "string.h"
21 #include "write.cmdline.h"
22 #include "list.h"
23 #include "sched.h"
24 #include "stdin.h"
25 #include "write.h"
26 #include "write_common.h"
27 #include "fd.h"
28 #include "error.h"
29
30 INIT_WRITE_ERRLISTS;
31
32 struct check_wav_task {
33         char *buf;
34         size_t *loaded;
35         int *eof;
36         unsigned channels;
37         unsigned sample_rate;
38         struct task task;
39 };
40
41 struct initial_delay_task {
42         struct timeval start_time;
43         struct task task;
44 };
45
46 struct gengetopt_args_info conf;
47 struct stdin_task sit;
48 struct check_wav_task cwt;
49 struct initial_delay_task idt;
50 static struct writer_node_group *wng;
51
52 #define WAV_HEADER_LEN 44
53
54 /**
55  * test if audio buffer contains a valid wave header
56  *
57  * \return If not, return -E_NO_WAV_HEADER, otherwise, return zero. If
58  * there is less than WAV_HEADER_LEN bytes awailable, return one.
59  */
60 static void check_wav_pre_select(struct sched *s, struct task *t)
61 {
62         struct check_wav_task *cwt = t->private_data;
63         unsigned char *a;
64
65         if (*cwt->loaded < WAV_HEADER_LEN) {
66                 t->ret = *cwt->eof? -E_PREMATURE_END : 1;
67                 return;
68         }
69         a = (unsigned char*)cwt->buf;
70         t->ret = -E_NO_WAV_HEADER;
71         if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F')
72                 return;
73         cwt->channels = (unsigned) a[22];
74         cwt->sample_rate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
75         *cwt->loaded -= WAV_HEADER_LEN;
76         memmove(cwt->buf, cwt->buf + WAV_HEADER_LEN, *cwt->loaded);
77         t->ret = 0;
78         PARA_INFO_LOG("channels: %d, sample_rate: %d\n", cwt->channels, cwt->sample_rate);
79 }
80
81 static void initial_delay_pre_select(struct sched *s, struct task *t)
82 {
83         struct initial_delay_task *idt = t->private_data;
84         struct timeval diff;
85
86         PARA_ERROR_LOG("task %p, ret: %d\n", t, t->ret);
87         t->ret = -E_NO_DELAY;
88         if (!idt->start_time.tv_sec && !idt->start_time.tv_usec)
89                 return;
90         t->ret = 0; /* timeout */
91         if (tv_diff(&s->now, &idt->start_time, &diff) > 0)
92                 return;
93         t->ret = 1;
94         if (tv_diff(&s->timeout , &diff, NULL) > 0)
95                 s->timeout = diff;
96 }
97
98 void para_log(int ll, const char* fmt,...)
99 {
100         va_list argp;
101
102         if (ll < conf.loglevel_arg)
103                 return;
104         va_start(argp, fmt);
105         vfprintf(stderr, fmt, argp);
106         va_end(argp);
107 }
108
109 static struct writer_node_group *check_args(void)
110 {
111         int i, ret = -E_WRITE_SYNTAX;
112         struct writer_node_group *wng = NULL;
113
114         if (conf.list_writers_given) {
115                 char *msg = NULL;
116                 FOR_EACH_WRITER(i) {
117                         char *tmp = make_message("%s%s%s",
118                                 i? msg : "",
119                                 i? " " : "",
120                                 writer_names[i]);
121                         free(msg);
122                         msg = tmp;
123                 }
124                 fprintf(stderr, "%s\n", msg);
125                 free(msg);
126                 exit(EXIT_SUCCESS);
127         }
128         if (conf.prebuffer_arg < 0 || conf.prebuffer_arg > 100)
129                 goto out;
130         if (conf.start_time_given) {
131                 long unsigned sec, usec;
132                 if (sscanf(conf.start_time_arg, "%lu:%lu",
133                                 &sec, &usec) != 2)
134                         goto out;
135                 idt.start_time.tv_sec = sec;
136                 idt.start_time.tv_usec = usec;
137         }
138         if (!conf.writer_given) {
139                 wng = setup_default_wng();
140                 ret = 1;
141                 goto out;
142         }
143         wng = wng_new(conf.writer_given);
144         for (i = 0; i < conf.writer_given; i++) {
145                 ret = check_writer_arg(conf.writer_arg[i]);
146                 if (ret < 0)
147                         goto out;
148                 wng->writer_nodes[i].writer = &writers[ret];
149         }
150         ret = 1;
151 out:
152         if (ret > 0) {
153                 return wng;
154         }
155         free(wng);
156         return NULL;
157 }
158
159 static void idt_error_handler(struct task *t)
160 {
161         PARA_ERROR_LOG("task %p, ret: %d\n", t, t->ret);
162         int ret;
163         unregister_task(t);
164         wng->buf = sit.buf;
165         wng->loaded = &sit.loaded;
166         wng->eof = &sit.eof;
167         ret = wng_open(wng);
168         if (ret < 0) {
169                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
170                 exit(EXIT_FAILURE);
171         }
172 }
173
174 static void cwt_error_handler(struct task *t)
175 {
176         PARA_ERROR_LOG("task %p, ret: %d\n", t, t->ret);
177         if (t->ret < 0) {
178                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret));
179                 if (t->ret != -E_NO_WAV_HEADER && t->ret != -E_PRE_EOF)
180                         exit(EXIT_FAILURE);
181                 if (t->ret == -E_PRE_EOF) {
182                         conf.channels_arg = cwt.channels;
183                         conf.sample_rate_arg = cwt.sample_rate;
184                 }
185         }
186         unregister_task(t);
187         idt.task.pre_select = initial_delay_pre_select;
188         idt.task.private_data = &idt;
189         idt.task.error_handler = idt_error_handler;
190         idt.task.flags = PRE_EOF_IS_ERROR;
191         register_task(&idt.task);
192 }
193
194 static void stdin_error_handler(struct task *t)
195 {
196         unregister_task(t);
197         PARA_INFO_LOG("task %p, ret: %d\n", t, t->ret);
198         if (t->ret < 0)
199                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret));
200 }
201
202 int main(int argc, char *argv[])
203 {
204         int ret = -E_WRITE_SYNTAX;
205         struct sched s;
206
207         cmdline_parser(argc, argv, &conf);
208         wng = check_args();
209         if (!wng)
210                 goto out;
211         init_supported_writers();
212         init_sched();
213
214         sit.bufsize = 16 * 1024,
215         sit.buf = para_malloc(16 * 1024),
216         sit.loaded = 0,
217         sit.task.pre_select = stdin_pre_select;
218         sit.task.post_select = stdin_post_select;
219         sit.task.error_handler = stdin_error_handler;
220         sit.task.flags = POST_EOF_IS_ERROR;
221         sit.task.private_data = &sit;
222         register_task(&sit.task);
223
224         cwt.task.pre_select = check_wav_pre_select;
225         cwt.task.private_data = &cwt;
226         cwt.task.error_handler = cwt_error_handler;
227         cwt.buf = sit.buf;
228         cwt.loaded = &sit.loaded;
229         cwt.eof = &sit.eof;
230         cwt.task.flags = PRE_EOF_IS_ERROR;
231         register_task(&cwt.task);
232
233         s.default_timeout.tv_sec = 1;
234         s.default_timeout.tv_usec = 0;
235         ret = sched(&s);
236
237 out:
238         if (ret < 0) {
239                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
240                 ret = EXIT_FAILURE;
241         } else
242                 ret = EXIT_SUCCESS;
243         return ret;
244 }