2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file write.c Paraslash's standalone wav/raw player. */
10 #include <sys/types.h>
16 #include "write.cmdline.h"
21 #include "buffer_tree.h"
23 #include "write_common.h"
29 /** Check if given buffer contains a valid wave header. */
30 struct check_wav_task {
31 /** The buffer to check. */
33 /** Number of bytes loaded in \a buf. */
35 /** Non-zero if an error occurred or end of file was reached. */
37 /** Number of channels specified in wav header given by \a buf. */
39 /** Sample rate specified in wav header given by \a buf. */
41 /** The task structure used by the scheduler. */
45 enum check_wav_state {
51 struct check_wav_task_btr {
53 /** Number of channels specified in wav header given by \a buf. */
55 /** Sample rate specified in wav header given by \a buf. */
57 /** The task structure used by the scheduler. */
59 struct btr_node *btrn;
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. */
70 static struct write_args_info conf;
72 static struct stdin_task sit;
74 static struct check_wav_task the_check_wav_task;
75 static struct initial_delay_task the_initial_delay_task;
77 static struct writer_node_group *wng;
79 /** Length of a standard wav header. */
80 #define WAV_HEADER_LEN 44
83 * Test if audio buffer contains a valid wave header.
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.
88 static void check_wav_pre_select(__a_unused struct sched *s, struct task *t)
90 struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
94 if (*cwt->loaded < WAV_HEADER_LEN) {
95 if (*cwt->input_error < 0)
96 t->error = *cwt->input_error;
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;
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);
114 wng->channels = &cwt->channels;
115 wng->samplerate = &cwt->samplerate;
119 s->timeout.tv_sec = 0;
120 s->timeout.tv_usec = 1;
123 static void check_wav_pre_select_btr(__a_unused struct sched *s, struct task *t)
125 struct check_wav_task_btr *cwt = container_of(t, struct check_wav_task_btr, task);
127 if (btr_get_input_queue_size(cwt->btrn) < WAV_HEADER_LEN)
129 s->timeout.tv_sec = 0;
130 s->timeout.tv_usec = 1;
133 static int check_wav_exec(struct btr_node *btrn, const char *cmd, char **result)
135 struct check_wav_task_btr *cwt = btr_context(btrn);
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);
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);
150 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
153 static void check_wav_post_select_btr(__a_unused struct sched *s, struct task *t)
155 struct check_wav_task_btr *cwt = container_of(t, struct check_wav_task_btr, task);
157 size_t sz = btr_get_input_queue_size(cwt->btrn);
160 if (cwt->state != CWS_NEED_HEADER)
162 if (sz < WAV_HEADER_LEN) {
163 if (!btr_no_parent(cwt->btrn))
166 cwt->state = CWS_NO_HEADER;
169 t->error = -E_WRITE_EOF;
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");
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 PARA_INFO_LOG("channels: %d, sample rate: %d\n", cwt->channels, cwt->samplerate);
187 btr_consume(cwt->btrn, WAV_HEADER_LEN);
190 btr_pushdown(cwt->btrn);
192 if (btr_no_parent(cwt->btrn))
193 t->error = -E_WRITE_EOF;
197 btr_remove_node(cwt->btrn);
200 static void initial_delay_pre_select(struct sched *s, struct task *t)
202 struct initial_delay_task *idt = container_of(t, struct initial_delay_task, task);
205 if (!idt->start_time.tv_sec && !idt->start_time.tv_usec) {
206 t->error = -E_NO_DELAY;
207 goto register_check_wav;
209 if (tv_diff(now, &idt->start_time, &diff) > 0) {
210 t->error = -E_DELAY_TIMEOUT;
211 goto register_check_wav;
213 if (tv_diff(&s->timeout , &diff, NULL) > 0)
217 register_task(&the_check_wav_task.task);
218 s->timeout.tv_sec = 0;
219 s->timeout.tv_usec = 1;
223 INIT_STDERR_LOGGING(loglevel)
225 static struct writer_node_group *check_args(void)
227 int i, ret = -E_WRITE_SYNTAX;
228 struct writer_node_group *g = NULL;
229 struct initial_delay_task *idt = &the_initial_delay_task;
231 loglevel = get_loglevel_by_name(conf.loglevel_arg);
232 if (conf.start_time_given) {
233 long unsigned sec, usec;
234 if (sscanf(conf.start_time_arg, "%lu:%lu",
237 idt->start_time.tv_sec = sec;
238 idt->start_time.tv_usec = usec;
240 if (!conf.writer_given) {
241 g = setup_default_wng();
245 g = wng_new(conf.writer_given);
246 ret = -E_WRITE_SYNTAX;
247 for (i = 0; i < conf.writer_given; i++) {
249 g->writer_nodes[i].conf = check_writer_arg(
250 conf.writer_arg[i], &writer_num);
251 if (!g->writer_nodes[i].conf)
253 g->writer_nodes[i].writer_num = writer_num;
263 __noreturn static void print_help_and_die(void)
265 int d = conf.detailed_help_given;
266 const char **p = d? write_args_info_detailed_help
267 : write_args_info_help;
269 printf_or_die("%s\n\n", WRITE_CMDLINE_PARSER_PACKAGE "-"
270 WRITE_CMDLINE_PARSER_VERSION);
271 printf_or_die("%s\n\n", write_args_info_usage);
273 printf_or_die("%s\n", *p);
274 print_writer_helps(d);
279 TODO: check wav, initial delay, multiple writers, non-default writers
281 static int main_btr(struct sched *s)
284 struct check_wav_task_btr _cwt, *cwt = &_cwt;
285 struct writer_node *wns;
287 loglevel = get_loglevel_by_name(conf.loglevel_arg);
288 sit.btrn = btr_new_node("stdin", NULL /* stdin has no parent */, NULL, NULL);
289 stdin_set_defaults(&sit);
290 register_task(&sit.task);
292 cwt->state = CWS_NEED_HEADER;
293 cwt->btrn = btr_new_node("check wav", sit.btrn, check_wav_exec, cwt);
294 sprintf(cwt->task.status, "check wav");
295 cwt->task.pre_select = check_wav_pre_select_btr;
296 cwt->task.post_select = check_wav_post_select_btr;
298 register_task(&cwt->task);
300 ret = -E_WRITE_SYNTAX;
301 if (!conf.writer_given) {
303 wns = para_calloc(sizeof(*wns));
304 ret = setup_writer_node(NULL, cwt->btrn, wns);
309 wns = para_calloc(conf.writer_given * sizeof(*wns));
310 for (i = 0; i < conf.writer_given; i++) {
311 ret = setup_writer_node(conf.writer_arg[i],
318 s->default_timeout.tv_sec = 10;
319 s->default_timeout.tv_usec = 50000;
322 for (i--; i >= 0; i--) {
323 struct writer_node *wn = wns + i;
324 struct writer *w = writers + wn->writer_num;
327 btr_free_node(wn->btrn);
332 btr_free_node(cwt->btrn);
337 * Para_write's main function.
339 * \param argc The usual argument counter.
340 * \param argv The usual argument vector.
342 * It registers the stdin task, the check_wav_task, the task for initial delay
343 * and all tasks for actually writing out the stream.
345 * \return \p EXIT_SUCCESS or EXIT_FAILURE
347 int main(int argc, char *argv[])
349 int ret = -E_WRITE_SYNTAX;
350 static struct sched s;
351 struct check_wav_task *cwt = &the_check_wav_task;
352 struct initial_delay_task *idt = &the_initial_delay_task;
355 write_cmdline_parser(argc, argv, &conf);
356 HANDLE_VERSION_FLAG("write", conf);
357 if (conf.help_given || conf.detailed_help_given)
358 print_help_and_die();
360 if (conf.buffer_tree_given) {
367 stdin_set_defaults(&sit);
368 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
369 if (conf.bufsize_arg < 0)
371 if (conf.bufsize_arg >= INT_MAX / 1024)
373 sit.bufsize = conf.bufsize_arg * 1024;
374 sit.buf = para_malloc(sit.bufsize);
376 wng->bufp = &sit.buf;
377 wng->loaded = &sit.loaded;
378 wng->input_error = &sit.task.error;
380 register_task(&sit.task);
383 cwt->loaded = &sit.loaded;
384 cwt->input_error = &sit.task.error;
385 sprintf(cwt->task.status, "check wav");
386 cwt->task.pre_select = check_wav_pre_select;
388 idt->task.pre_select = initial_delay_pre_select;
389 sprintf(idt->task.status, "initial_delay");
390 register_task(&idt->task);
392 s.default_timeout.tv_sec = 10;
393 s.default_timeout.tv_usec = 0;
398 PARA_ERROR_LOG("%s\n", para_strerror(-ret));