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"
22 #include "write_common.h"
25 #include "buffer_tree.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 /** Delay writing until given time. */
46 struct initial_delay_task {
47 /** The time the first data should be written out. */
48 struct timeval start_time;
49 /** The task structure for this task. */
53 static struct write_args_info conf;
55 static struct stdin_task sit;
57 static struct check_wav_task the_check_wav_task;
58 static struct initial_delay_task the_initial_delay_task;
60 static struct writer_node_group *wng;
62 /** Length of a standard wav header. */
63 #define WAV_HEADER_LEN 44
66 * Test if audio buffer contains a valid wave header.
68 * \return If not, return -E_NO_WAV_HEADER, otherwise, return zero. If
69 * there is less than WAV_HEADER_LEN bytes available, return one.
71 static void check_wav_pre_select(__a_unused struct sched *s, struct task *t)
73 struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
77 if (*cwt->loaded < WAV_HEADER_LEN) {
78 if (*cwt->input_error < 0)
79 t->error = *cwt->input_error;
83 cwt->samplerate = 44100;
84 a = (unsigned char*)cwt->buf;
85 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F') {
86 PARA_NOTICE_LOG("wav header not found\n");
87 t->error = -E_NO_WAV_HEADER;
90 cwt->channels = (unsigned) a[22];
91 cwt->samplerate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
92 *cwt->loaded -= WAV_HEADER_LEN;
93 memmove(cwt->buf, cwt->buf + WAV_HEADER_LEN, *cwt->loaded);
94 t->error = -E_WAV_HEADER_SUCCESS;
95 PARA_INFO_LOG("channels: %d, sample rate: %d\n", cwt->channels, cwt->samplerate);
97 wng->channels = &cwt->channels;
98 wng->samplerate = &cwt->samplerate;
102 s->timeout.tv_sec = 0;
103 s->timeout.tv_usec = 1;
106 static void initial_delay_pre_select(struct sched *s, struct task *t)
108 struct initial_delay_task *idt = container_of(t, struct initial_delay_task, task);
111 if (!idt->start_time.tv_sec && !idt->start_time.tv_usec) {
112 t->error = -E_NO_DELAY;
113 goto register_check_wav;
115 if (tv_diff(now, &idt->start_time, &diff) > 0) {
116 t->error = -E_DELAY_TIMEOUT;
117 goto register_check_wav;
119 if (tv_diff(&s->timeout , &diff, NULL) > 0)
123 register_task(&the_check_wav_task.task);
124 s->timeout.tv_sec = 0;
125 s->timeout.tv_usec = 1;
129 INIT_STDERR_LOGGING(loglevel)
131 static struct writer_node_group *check_args(void)
133 int i, ret = -E_WRITE_SYNTAX;
134 struct writer_node_group *g = NULL;
135 struct initial_delay_task *idt = &the_initial_delay_task;
137 loglevel = get_loglevel_by_name(conf.loglevel_arg);
138 if (conf.start_time_given) {
139 long unsigned sec, usec;
140 if (sscanf(conf.start_time_arg, "%lu:%lu",
143 idt->start_time.tv_sec = sec;
144 idt->start_time.tv_usec = usec;
146 if (!conf.writer_given) {
147 g = setup_default_wng();
151 g = wng_new(conf.writer_given);
152 ret = -E_WRITE_SYNTAX;
153 for (i = 0; i < conf.writer_given; i++) {
155 g->writer_nodes[i].conf = check_writer_arg(
156 conf.writer_arg[i], &writer_num);
157 if (!g->writer_nodes[i].conf)
159 g->writer_nodes[i].writer_num = writer_num;
169 __noreturn static void print_help_and_die(void)
171 int d = conf.detailed_help_given;
172 const char **p = d? write_args_info_detailed_help
173 : write_args_info_help;
175 printf_or_die("%s\n\n", WRITE_CMDLINE_PARSER_PACKAGE "-"
176 WRITE_CMDLINE_PARSER_VERSION);
177 printf_or_die("%s\n\n", write_args_info_usage);
179 printf_or_die("%s\n", *p);
180 print_writer_helps(d);
185 TODO: check wav, initial delay, multiple writers, non-default writers
187 static int main_btr(struct sched *s)
189 struct writer_node *wn = para_malloc(sizeof(*wn));
190 struct writer *w = writers + DEFAULT_WRITER;
193 sit.btrn = btr_new_node("stdin", NULL /* stdin has no parent */, NULL);
194 stdin_set_defaults(&sit);
195 register_task(&sit.task);
197 wn->writer_num = DEFAULT_WRITER;
198 wn->conf = writers[DEFAULT_WRITER].parse_config("-B");
199 wn->btrn = btr_new_node("writer", sit.btrn, NULL);
200 sprintf(wn->task.status, "some writer");
202 wn->task.post_select = w->post_select_btr;
203 wn->task.pre_select = w->pre_select_btr;
204 register_task(&wn->task);
208 s->default_timeout.tv_sec = 10;
209 s->default_timeout.tv_usec = 50000;
216 * Para_write's main function.
218 * \param argc The usual argument counter.
219 * \param argv The usual argument vector.
221 * It registers the stdin task, the check_wav_task, the task for initial delay
222 * and all tasks for actually writing out the stream.
224 * \return \p EXIT_SUCCESS or EXIT_FAILURE
226 int main(int argc, char *argv[])
228 int ret = -E_WRITE_SYNTAX;
229 static struct sched s;
230 struct check_wav_task *cwt = &the_check_wav_task;
231 struct initial_delay_task *idt = &the_initial_delay_task;
234 write_cmdline_parser(argc, argv, &conf);
235 HANDLE_VERSION_FLAG("write", conf);
236 if (conf.help_given || conf.detailed_help_given)
237 print_help_and_die();
239 if (conf.buffer_tree_given) {
246 stdin_set_defaults(&sit);
247 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
248 if (conf.bufsize_arg < 0)
250 if (conf.bufsize_arg >= INT_MAX / 1024)
252 sit.bufsize = conf.bufsize_arg * 1024;
253 sit.buf = para_malloc(sit.bufsize);
255 wng->bufp = &sit.buf;
256 wng->loaded = &sit.loaded;
257 wng->input_error = &sit.task.error;
259 register_task(&sit.task);
262 cwt->loaded = &sit.loaded;
263 cwt->input_error = &sit.task.error;
264 sprintf(cwt->task.status, "check wav");
265 cwt->task.pre_select = check_wav_pre_select;
267 idt->task.pre_select = initial_delay_pre_select;
268 sprintf(idt->task.status, "initial_delay");
269 register_task(&idt->task);
271 s.default_timeout.tv_sec = 10;
272 s.default_timeout.tv_usec = 0;
277 PARA_ERROR_LOG("%s\n", para_strerror(-ret));