c3848946c8b124e1a0ba0f0672fbbf475d6de2a4
[paraslash.git] / write.c
1 /*
2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file write.c Paraslash's standalone wav/raw player. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11 #include <dirent.h>
12 #include <stdbool.h>
13
14 #include "para.h"
15 #include "string.h"
16 #include "write.cmdline.h"
17 #include "list.h"
18 #include "sched.h"
19 #include "ggo.h"
20 #include "stdin.h"
21 #include "buffer_tree.h"
22 #include "write.h"
23 #include "write_common.h"
24 #include "fd.h"
25 #include "error.h"
26
27 INIT_WRITE_ERRLISTS;
28
29 /** Check if given buffer contains a valid wave header. */
30 struct check_wav_task {
31 /** The buffer to check. */
32 char *buf;
33 /** Number of bytes loaded in \a buf. */
34 size_t *loaded;
35 /** Non-zero if an error occurred or end of file was reached. */
36 int *input_error;
37 /** Number of channels specified in wav header given by \a buf. */
38 unsigned channels;
39 /** Sample rate specified in wav header given by \a buf. */
40 unsigned samplerate;
41 /** The task structure used by the scheduler. */
42 struct task task;
43 };
44
45 enum check_wav_state {
46 CWS_NEED_HEADER,
47 CWS_HAVE_HEADER,
48 CWS_NO_HEADER,
49 };
50
51 struct check_wav_task_btr {
52 int state;
53 /** Number of channels specified in wav header given by \a buf. */
54 unsigned channels;
55 /** Sample rate specified in wav header given by \a buf. */
56 unsigned samplerate;
57 /** The task structure used by the scheduler. */
58 struct task task;
59 struct btr_node *btrn;
60 };
61
62 /** Delay writing until given time. */
63 struct initial_delay_task {
64 /** The time the first data should be written out. */
65 struct timeval start_time;
66 /** The task structure for this task. */
67 struct task task;
68 };
69
70 static struct write_args_info conf;
71
72 static struct stdin_task sit;
73
74 static struct check_wav_task the_check_wav_task;
75 static struct initial_delay_task the_initial_delay_task;
76
77 static struct writer_node_group *wng;
78
79 /** Length of a standard wav header. */
80 #define WAV_HEADER_LEN 44
81
82 /**
83 * Test if audio buffer contains a valid wave header.
84 *
85 * \return If not, return -E_NO_WAV_HEADER, otherwise, return zero. If
86 * there is less than WAV_HEADER_LEN bytes available, return one.
87 */
88 static void check_wav_pre_select(__a_unused struct sched *s, struct task *t)
89 {
90 struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
91 unsigned char *a;
92 int ret;
93
94 if (*cwt->loaded < WAV_HEADER_LEN) {
95 if (*cwt->input_error < 0)
96 t->error = *cwt->input_error;
97 return;
98 }
99 cwt->channels = 2;
100 cwt->samplerate = 44100;
101 a = (unsigned char*)cwt->buf;
102 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F') {
103 PARA_NOTICE_LOG("wav header not found\n");
104 t->error = -E_NO_WAV_HEADER;
105 goto out;
106 }
107 cwt->channels = (unsigned) a[22];
108 cwt->samplerate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
109 *cwt->loaded -= WAV_HEADER_LEN;
110 memmove(cwt->buf, cwt->buf + WAV_HEADER_LEN, *cwt->loaded);
111 t->error = -E_WAV_HEADER_SUCCESS;
112 PARA_INFO_LOG("channels: %d, sample rate: %d\n", cwt->channels, cwt->samplerate);
113 out:
114 wng->channels = &cwt->channels;
115 wng->samplerate = &cwt->samplerate;
116 ret = wng_open(wng);
117 if (ret < 0)
118 t->error = ret;
119 s->timeout.tv_sec = 0;
120 s->timeout.tv_usec = 1;
121 }
122
123 static void check_wav_pre_select_btr(__a_unused struct sched *s, struct task *t)
124 {
125 struct check_wav_task_btr *cwt = container_of(t, struct check_wav_task_btr, task);
126
127 if (btr_get_input_queue_size(cwt->btrn) < WAV_HEADER_LEN)
128 return;
129 s->timeout.tv_sec = 0;
130 s->timeout.tv_usec = 1;
131 }
132
133 static int check_wav_exec(struct btr_node *btrn, const char *cmd, char **result)
134 {
135 struct check_wav_task_btr *cwt = btr_context(btrn);
136
137
138 if (!strcmp(cmd, "samplerate")) {
139 if (cwt->state != CWS_HAVE_HEADER)
140 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
141 *result = make_message("%d", cwt->samplerate);
142 return 1;
143 }
144 if (!strcmp(cmd, "channels")) {
145 if (cwt->state != CWS_HAVE_HEADER)
146 return -ERRNO_TO_PARA_ERROR(ENAVAIL);
147 *result = make_message("%d", cwt->channels);
148 return 1;
149 }
150 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
151 }
152
153 static void check_wav_post_select_btr(__a_unused struct sched *s, struct task *t)
154 {
155 struct check_wav_task_btr *cwt = container_of(t, struct check_wav_task_btr, task);
156 unsigned char *a;
157 size_t sz = btr_get_input_queue_size(cwt->btrn);
158
159 t->error = 0;
160 if (cwt->state != CWS_NEED_HEADER)
161 goto out;
162 if (sz < WAV_HEADER_LEN) {
163 if (!btr_no_parent(cwt->btrn))
164 return;
165 if (sz != 0) {
166 cwt->state = CWS_NO_HEADER;
167 goto out;
168 }
169 t->error = -E_WRITE_EOF;
170 goto err;
171 }
172 cwt->channels = 2;
173 cwt->samplerate = 44100;
174 btr_next_buffer(cwt->btrn, (char **)&a);
175 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F') {
176 PARA_NOTICE_LOG("wav header not found\n");
177 cwt->state = CWS_NO_HEADER;
178 sprintf(t->status, "check wav: no header");
179 goto consume;
180 }
181 PARA_INFO_LOG("found wav header\n");
182 cwt->state = CWS_HAVE_HEADER;
183 sprintf(t->status, "check wav: have header");
184 cwt->channels = (unsigned) a[22];
185 cwt->samplerate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
186 consume:
187 PARA_INFO_LOG("channels: %d, sample rate: %d\n", cwt->channels, cwt->samplerate);
188 btr_consume(cwt->btrn, WAV_HEADER_LEN);
189 out:
190 if (sz) {
191 btr_pushdown(cwt->btrn);
192 s->timeout.tv_sec = 0;
193 s->timeout.tv_usec = 1;
194 } else {
195 if (btr_no_parent(cwt->btrn))
196 t->error = -E_WRITE_EOF;
197 }
198 err:
199 if (t->error < 0)
200 btr_del_node(cwt->btrn);
201 }
202
203 static void initial_delay_pre_select(struct sched *s, struct task *t)
204 {
205 struct initial_delay_task *idt = container_of(t, struct initial_delay_task, task);
206 struct timeval diff;
207
208 if (!idt->start_time.tv_sec && !idt->start_time.tv_usec) {
209 t->error = -E_NO_DELAY;
210 goto register_check_wav;
211 }
212 if (tv_diff(now, &idt->start_time, &diff) > 0) {
213 t->error = -E_DELAY_TIMEOUT;
214 goto register_check_wav;
215 }
216 if (tv_diff(&s->timeout , &diff, NULL) > 0)
217 s->timeout = diff;
218 return;
219 register_check_wav:
220 register_task(&the_check_wav_task.task);
221 s->timeout.tv_sec = 0;
222 s->timeout.tv_usec = 1;
223 }
224
225 static int loglevel;
226 INIT_STDERR_LOGGING(loglevel)
227
228 static struct writer_node_group *check_args(void)
229 {
230 int i, ret = -E_WRITE_SYNTAX;
231 struct writer_node_group *g = NULL;
232 struct initial_delay_task *idt = &the_initial_delay_task;
233
234 loglevel = get_loglevel_by_name(conf.loglevel_arg);
235 if (conf.start_time_given) {
236 long unsigned sec, usec;
237 if (sscanf(conf.start_time_arg, "%lu:%lu",
238 &sec, &usec) != 2)
239 goto out;
240 idt->start_time.tv_sec = sec;
241 idt->start_time.tv_usec = usec;
242 }
243 if (!conf.writer_given) {
244 g = setup_default_wng();
245 ret = 1;
246 goto out;
247 }
248 g = wng_new(conf.writer_given);
249 ret = -E_WRITE_SYNTAX;
250 for (i = 0; i < conf.writer_given; i++) {
251 int writer_num;
252 g->writer_nodes[i].conf = check_writer_arg(
253 conf.writer_arg[i], &writer_num);
254 if (!g->writer_nodes[i].conf)
255 goto out;
256 g->writer_nodes[i].writer_num = writer_num;
257 }
258 ret = 1;
259 out:
260 if (ret > 0)
261 return g;
262 free(g);
263 return NULL;
264 }
265
266 __noreturn static void print_help_and_die(void)
267 {
268 int d = conf.detailed_help_given;
269 const char **p = d? write_args_info_detailed_help
270 : write_args_info_help;
271
272 printf_or_die("%s\n\n", WRITE_CMDLINE_PARSER_PACKAGE "-"
273 WRITE_CMDLINE_PARSER_VERSION);
274 printf_or_die("%s\n\n", write_args_info_usage);
275 for (; *p; p++)
276 printf_or_die("%s\n", *p);
277 print_writer_helps(d);
278 exit(0);
279 }
280
281 /*
282 TODO: check wav, initial delay, multiple writers, non-default writers
283 */
284 static int main_btr(struct sched *s)
285 {
286 int i, ret;
287 struct check_wav_task_btr _cwt, *cwt = &_cwt;
288 struct writer_node **wns;
289
290 sit.btrn = btr_new_node("stdin", NULL /* stdin has no parent */, NULL, NULL);
291 stdin_set_defaults(&sit);
292 register_task(&sit.task);
293
294 cwt->state = CWS_NEED_HEADER;
295 cwt->btrn = btr_new_node("check wav", sit.btrn, check_wav_exec, cwt);
296 sprintf(cwt->task.status, "check wav");
297 cwt->task.pre_select = check_wav_pre_select_btr;
298 cwt->task.post_select = check_wav_post_select_btr;
299 cwt->task.error = 0;
300 register_task(&cwt->task);
301
302 PARA_CRIT_LOG("writers:\n");
303
304 ret = -E_WRITE_SYNTAX;
305 if (!conf.writer_given) {
306 i = 0;
307 wns = para_malloc(sizeof(*wns));
308 wns[0] = setup_writer_node(NULL, cwt->btrn);
309 if (!wns[0])
310 goto out;
311 } else {
312 wns = para_malloc(conf.writer_given * sizeof(*wns));
313 for (i = 0; i < conf.writer_given; i++) {
314 PARA_CRIT_LOG("i: %d\n", i);
315 wns[i] = setup_writer_node(conf.writer_arg[i],
316 cwt->btrn);
317 if (!wns[i])
318 goto out;
319 }
320 }
321
322 s->default_timeout.tv_sec = 10;
323 s->default_timeout.tv_usec = 50000;
324 ret = schedule(s);
325 out:
326 for (i--; i >= 0; i--) {
327 struct writer_node *wn = wns[i];
328 struct writer *w = writers + wn->writer_num;
329 w->close(wn);
330 free(wn->conf); /* FIXME should call gengetopt cleanup funtion */
331 free(wn);
332 }
333 free(wns);
334 return ret;
335 }
336
337 /**
338 * Para_write's main function.
339 *
340 * \param argc The usual argument counter.
341 * \param argv The usual argument vector.
342 *
343 * It registers the stdin task, the check_wav_task, the task for initial delay
344 * and all tasks for actually writing out the stream.
345 *
346 * \return \p EXIT_SUCCESS or EXIT_FAILURE
347 */
348 int main(int argc, char *argv[])
349 {
350 int ret = -E_WRITE_SYNTAX;
351 static struct sched s;
352 struct check_wav_task *cwt = &the_check_wav_task;
353 struct initial_delay_task *idt = &the_initial_delay_task;
354
355 writer_init();
356 write_cmdline_parser(argc, argv, &conf);
357 HANDLE_VERSION_FLAG("write", conf);
358 if (conf.help_given || conf.detailed_help_given)
359 print_help_and_die();
360
361 if (conf.buffer_tree_given) {
362 ret = main_btr(&s);
363 goto out;
364 }
365 wng = check_args();
366 if (!wng)
367 goto out;
368 stdin_set_defaults(&sit);
369 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
370 if (conf.bufsize_arg < 0)
371 goto out;
372 if (conf.bufsize_arg >= INT_MAX / 1024)
373 goto out;
374 sit.bufsize = conf.bufsize_arg * 1024;
375 sit.buf = para_malloc(sit.bufsize);
376
377 wng->bufp = &sit.buf;
378 wng->loaded = &sit.loaded;
379 wng->input_error = &sit.task.error;
380
381 register_task(&sit.task);
382
383 cwt->buf = sit.buf;
384 cwt->loaded = &sit.loaded;
385 cwt->input_error = &sit.task.error;
386 sprintf(cwt->task.status, "check wav");
387 cwt->task.pre_select = check_wav_pre_select;
388
389 idt->task.pre_select = initial_delay_pre_select;
390 sprintf(idt->task.status, "initial_delay");
391 register_task(&idt->task);
392
393 s.default_timeout.tv_sec = 10;
394 s.default_timeout.tv_usec = 0;
395 ret = schedule(&s);
396 wng_close(wng);
397 out:
398 if (ret < 0) {
399 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
400 exit(EXIT_FAILURE);
401 }
402 exit(EXIT_SUCCESS);
403 }