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. */
14 #include "write.cmdline.h"
20 #include "write_common.h"
26 /** Check if given buffer contains a valid wave header. */
27 struct check_wav_task {
28 /** The buffer to check. */
30 /** Number of bytes loaded in \a buf. */
32 /** Non-zero if an error occurred or end of file was reached. */
34 /** Number of channels specified in wav header given by \a buf. */
36 /** Sample rate specified in wav header given by \a buf. */
38 /** The task structure used by the scheduler. */
42 /** Delay writing until given time. */
43 struct initial_delay_task {
44 /** The time the first data should be written out. */
45 struct timeval start_time;
46 /** The task structure for this task. */
50 static struct write_args_info conf;
52 static struct stdin_task sit;
54 static struct check_wav_task the_check_wav_task;
55 static struct initial_delay_task the_initial_delay_task;
57 static struct writer_node_group *wng;
59 /** Length of a standard wav header. */
60 #define WAV_HEADER_LEN 44
63 * Test if audio buffer contains a valid wave header.
65 * \return If not, return -E_NO_WAV_HEADER, otherwise, return zero. If
66 * there is less than WAV_HEADER_LEN bytes available, return one.
68 static void check_wav_pre_select(__a_unused struct sched *s, struct task *t)
70 struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
74 if (*cwt->loaded < WAV_HEADER_LEN) {
75 if (*cwt->input_error < 0)
76 t->error = *cwt->input_error;
80 cwt->samplerate = 44100;
81 a = (unsigned char*)cwt->buf;
82 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F') {
83 PARA_NOTICE_LOG("wav header not found\n");
84 t->error = -E_NO_WAV_HEADER;
87 cwt->channels = (unsigned) a[22];
88 cwt->samplerate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
89 *cwt->loaded -= WAV_HEADER_LEN;
90 memmove(cwt->buf, cwt->buf + WAV_HEADER_LEN, *cwt->loaded);
91 t->error = -E_WAV_HEADER_SUCCESS;
92 PARA_INFO_LOG("channels: %d, sample rate: %d\n", cwt->channels, cwt->samplerate);
94 wng->channels = &cwt->channels;
95 wng->samplerate = &cwt->samplerate;
99 s->timeout.tv_sec = 0;
100 s->timeout.tv_usec = 1;
103 static void initial_delay_pre_select(struct sched *s, struct task *t)
105 struct initial_delay_task *idt = container_of(t, struct initial_delay_task, task);
108 if (!idt->start_time.tv_sec && !idt->start_time.tv_usec) {
109 t->error = -E_NO_DELAY;
110 goto register_check_wav;
112 if (tv_diff(now, &idt->start_time, &diff) > 0) {
113 t->error = -E_DELAY_TIMEOUT;
114 goto register_check_wav;
116 if (tv_diff(&s->timeout , &diff, NULL) > 0)
120 register_task(&the_check_wav_task.task);
121 s->timeout.tv_sec = 0;
122 s->timeout.tv_usec = 1;
126 INIT_STDERR_LOGGING(loglevel)
128 static struct writer_node_group *check_args(void)
130 int i, ret = -E_WRITE_SYNTAX;
131 struct writer_node_group *g = NULL;
132 struct initial_delay_task *idt = &the_initial_delay_task;
134 loglevel = get_loglevel_by_name(conf.loglevel_arg);
135 if (conf.start_time_given) {
136 long unsigned sec, usec;
137 if (sscanf(conf.start_time_arg, "%lu:%lu",
140 idt->start_time.tv_sec = sec;
141 idt->start_time.tv_usec = usec;
143 if (!conf.writer_given) {
144 g = setup_default_wng();
148 g = wng_new(conf.writer_given);
149 ret = -E_WRITE_SYNTAX;
150 for (i = 0; i < conf.writer_given; i++) {
152 g->writer_nodes[i].conf = check_writer_arg(
153 conf.writer_arg[i], &writer_num);
154 if (!g->writer_nodes[i].conf)
156 g->writer_nodes[i].writer = &writers[writer_num];
166 __noreturn static void print_help_and_die(void)
168 int d = conf.detailed_help_given;
169 const char **p = d? write_args_info_detailed_help
170 : write_args_info_help;
172 printf_or_die("%s\n\n", WRITE_CMDLINE_PARSER_PACKAGE "-"
173 WRITE_CMDLINE_PARSER_VERSION);
174 printf_or_die("%s\n\n", write_args_info_usage);
176 printf_or_die("%s\n", *p);
177 print_writer_helps(d);
182 * Para_write's main function.
184 * \param argc The usual argument counter.
185 * \param argv The usual argument vector.
187 * It registers the stdin task, the check_wav_task, the task for initial delay
188 * and all tasks for actually writing out the stream.
190 * \return \p EXIT_SUCCESS or EXIT_FAILURE
192 int main(int argc, char *argv[])
194 int ret = -E_WRITE_SYNTAX;
195 static struct sched s;
196 struct check_wav_task *cwt = &the_check_wav_task;
197 struct initial_delay_task *idt = &the_initial_delay_task;
200 write_cmdline_parser(argc, argv, &conf);
201 HANDLE_VERSION_FLAG("write", conf);
202 if (conf.help_given || conf.detailed_help_given)
203 print_help_and_die();
208 stdin_set_defaults(&sit);
209 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
210 if (conf.bufsize_arg < 0)
212 if (conf.bufsize_arg >= INT_MAX / 1024)
214 sit.bufsize = conf.bufsize_arg * 1024;
215 sit.buf = para_malloc(sit.bufsize);
218 wng->loaded = &sit.loaded;
219 wng->input_error = &sit.task.error;
221 register_task(&sit.task);
224 cwt->loaded = &sit.loaded;
225 cwt->input_error = &sit.task.error;
226 sprintf(cwt->task.status, "check wav");
227 cwt->task.pre_select = check_wav_pre_select;
229 idt->task.pre_select = initial_delay_pre_select;
230 sprintf(idt->task.status, "initial_delay");
231 register_task(&idt->task);
233 s.default_timeout.tv_sec = 10;
234 s.default_timeout.tv_usec = 0;
239 PARA_ERROR_LOG("%s\n", para_strerror(-ret));