buffer tree: Fix btr_splice_out_node().
[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 "write.h"
22 #include "write_common.h"
23 #include "fd.h"
24 #include "error.h"
25 #include "buffer_tree.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         if (!strcmp(cmd, "samplerate")) {
138                 if (cwt->state != CWS_HAVE_HEADER)
139                         return -ERRNO_TO_PARA_ERROR(ENAVAIL);
140                 *result = make_message("%d", cwt->samplerate);
141                 return 1;
142         }
143         if (!strcmp(cmd, "channels")) {
144                 if (cwt->state != CWS_HAVE_HEADER)
145                         return -ERRNO_TO_PARA_ERROR(ENAVAIL);
146                 *result = make_message("%d", cwt->samplerate);
147                 return 1;
148         }
149         return -ERRNO_TO_PARA_ERROR(ENOTSUP);
150 }
151
152 static void check_wav_post_select_btr(__a_unused struct sched *s, struct task *t)
153 {
154         struct check_wav_task_btr *cwt = container_of(t, struct check_wav_task_btr, task);
155         unsigned char *a;
156         size_t sz = btr_get_input_queue_size(cwt->btrn);
157
158         t->error = 0;
159         if (cwt->state != CWS_NEED_HEADER)
160                 goto out;
161         if (sz < WAV_HEADER_LEN) {
162                 if (!btr_no_parent(cwt->btrn))
163                         return;
164                 if (sz != 0) {
165                         cwt->state = CWS_NO_HEADER;
166                         goto out;
167                 }
168                 t->error = -E_WRITE_EOF;
169                 goto err;
170         }
171         cwt->channels = 2;
172         cwt->samplerate = 44100;
173         btr_next_buffer(cwt->btrn, (char **)&a);
174         if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F') {
175                 PARA_NOTICE_LOG("wav header not found\n");
176                 cwt->state = CWS_NO_HEADER;
177                 sprintf(t->status, "check wav: no header");
178                 goto consume;
179         }
180         PARA_INFO_LOG("found wav header\n");
181         cwt->state = CWS_HAVE_HEADER;
182         sprintf(t->status, "check wav: have header");
183         cwt->channels = (unsigned) a[22];
184         cwt->samplerate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
185 consume:
186         PARA_INFO_LOG("channels: %d, sample rate: %d\n", cwt->channels, cwt->samplerate);
187         btr_consume(cwt->btrn, WAV_HEADER_LEN);
188 out:
189         if (sz) {
190                 btr_pushdown(cwt->btrn);
191                 s->timeout.tv_sec = 0;
192                 s->timeout.tv_usec = 1;
193         } else {
194                 if (btr_no_parent(cwt->btrn))
195                         t->error = -E_WRITE_EOF;
196         }
197 err:
198         if (t->error < 0)
199                 btr_del_node(cwt->btrn);
200 }
201
202 static void initial_delay_pre_select(struct sched *s, struct task *t)
203 {
204         struct initial_delay_task *idt = container_of(t, struct initial_delay_task, task);
205         struct timeval diff;
206
207         if (!idt->start_time.tv_sec && !idt->start_time.tv_usec) {
208                 t->error = -E_NO_DELAY;
209                 goto register_check_wav;
210         }
211         if (tv_diff(now, &idt->start_time, &diff) > 0) {
212                 t->error = -E_DELAY_TIMEOUT;
213                 goto register_check_wav;
214         }
215         if (tv_diff(&s->timeout , &diff, NULL) > 0)
216                 s->timeout = diff;
217         return;
218 register_check_wav:
219         register_task(&the_check_wav_task.task);
220         s->timeout.tv_sec = 0;
221         s->timeout.tv_usec = 1;
222 }
223
224 static int loglevel;
225 INIT_STDERR_LOGGING(loglevel)
226
227 static struct writer_node_group *check_args(void)
228 {
229         int i, ret = -E_WRITE_SYNTAX;
230         struct writer_node_group *g = NULL;
231         struct initial_delay_task *idt = &the_initial_delay_task;
232
233         loglevel = get_loglevel_by_name(conf.loglevel_arg);
234         if (conf.start_time_given) {
235                 long unsigned sec, usec;
236                 if (sscanf(conf.start_time_arg, "%lu:%lu",
237                                 &sec, &usec) != 2)
238                         goto out;
239                 idt->start_time.tv_sec = sec;
240                 idt->start_time.tv_usec = usec;
241         }
242         if (!conf.writer_given) {
243                 g = setup_default_wng();
244                 ret = 1;
245                 goto out;
246         }
247         g = wng_new(conf.writer_given);
248         ret = -E_WRITE_SYNTAX;
249         for (i = 0; i < conf.writer_given; i++) {
250                 int writer_num;
251                 g->writer_nodes[i].conf = check_writer_arg(
252                         conf.writer_arg[i], &writer_num);
253                 if (!g->writer_nodes[i].conf)
254                         goto out;
255                 g->writer_nodes[i].writer_num = writer_num;
256         }
257         ret = 1;
258 out:
259         if (ret > 0)
260                 return g;
261         free(g);
262         return NULL;
263 }
264
265 __noreturn static void print_help_and_die(void)
266 {
267         int d = conf.detailed_help_given;
268         const char **p = d? write_args_info_detailed_help
269                 : write_args_info_help;
270
271         printf_or_die("%s\n\n", WRITE_CMDLINE_PARSER_PACKAGE "-"
272                 WRITE_CMDLINE_PARSER_VERSION);
273         printf_or_die("%s\n\n", write_args_info_usage);
274         for (; *p; p++)
275                 printf_or_die("%s\n", *p);
276         print_writer_helps(d);
277         exit(0);
278 }
279
280 /*
281  TODO: check wav, initial delay, multiple writers, non-default writers
282  */
283 static int main_btr(struct sched *s)
284 {
285         struct writer_node *wn = para_malloc(sizeof(*wn));
286         struct writer *w = writers + DEFAULT_WRITER;
287         int ret;
288         struct check_wav_task_btr _cwt, *cwt = &_cwt;
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         register_task(&cwt->task);
300
301         wn->writer_num = DEFAULT_WRITER;
302         wn->conf = writers[DEFAULT_WRITER].parse_config("-B");
303         wn->btrn = btr_new_node("writer", cwt->btrn, NULL, NULL);
304         sprintf(wn->task.status, "some writer");
305         w->open(wn);
306         wn->task.post_select = w->post_select_btr;
307         wn->task.pre_select = w->pre_select_btr;
308         register_task(&wn->task);
309
310
311
312         s->default_timeout.tv_sec = 10;
313         s->default_timeout.tv_usec = 50000;
314         ret = schedule(s);
315         w->close(wn);
316         return ret;
317 }
318
319 /**
320  * Para_write's main function.
321  *
322  * \param argc The usual argument counter.
323  * \param argv The usual argument vector.
324  *
325  * It registers the stdin task, the check_wav_task, the task for initial delay
326  * and all tasks for actually writing out the stream.
327  *
328  * \return \p EXIT_SUCCESS or EXIT_FAILURE
329  */
330 int main(int argc, char *argv[])
331 {
332         int ret = -E_WRITE_SYNTAX;
333         static struct sched s;
334         struct check_wav_task *cwt = &the_check_wav_task;
335         struct initial_delay_task *idt = &the_initial_delay_task;
336
337         writer_init();
338         write_cmdline_parser(argc, argv, &conf);
339         HANDLE_VERSION_FLAG("write", conf);
340         if (conf.help_given || conf.detailed_help_given)
341                 print_help_and_die();
342
343         if (conf.buffer_tree_given) {
344                 ret = main_btr(&s);
345                 goto out;
346         }
347         wng = check_args();
348         if (!wng)
349                 goto out;
350         stdin_set_defaults(&sit);
351         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
352         if (conf.bufsize_arg < 0)
353                 goto out;
354         if (conf.bufsize_arg >= INT_MAX / 1024)
355                 goto out;
356         sit.bufsize = conf.bufsize_arg * 1024;
357         sit.buf = para_malloc(sit.bufsize);
358
359         wng->bufp = &sit.buf;
360         wng->loaded = &sit.loaded;
361         wng->input_error = &sit.task.error;
362
363         register_task(&sit.task);
364
365         cwt->buf = sit.buf;
366         cwt->loaded = &sit.loaded;
367         cwt->input_error = &sit.task.error;
368         sprintf(cwt->task.status, "check wav");
369         cwt->task.pre_select = check_wav_pre_select;
370
371         idt->task.pre_select = initial_delay_pre_select;
372         sprintf(idt->task.status, "initial_delay");
373         register_task(&idt->task);
374
375         s.default_timeout.tv_sec = 10;
376         s.default_timeout.tv_usec = 0;
377         ret = schedule(&s);
378         wng_close(wng);
379 out:
380         if (ret < 0) {
381                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
382                 exit(EXIT_FAILURE);
383         }
384         exit(EXIT_SUCCESS);
385 }