2 * Copyright (C) 2005-2008 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;
125 INIT_STDERR_LOGGING(conf.loglevel_arg)
127 static struct writer_node_group *check_args(void)
129 int i, ret = -E_WRITE_SYNTAX;
130 struct writer_node_group *g = NULL;
131 struct initial_delay_task *idt = &the_initial_delay_task;
133 if (conf.start_time_given) {
134 long unsigned sec, usec;
135 if (sscanf(conf.start_time_arg, "%lu:%lu",
138 idt->start_time.tv_sec = sec;
139 idt->start_time.tv_usec = usec;
141 if (!conf.writer_given) {
142 g = setup_default_wng();
146 g = wng_new(conf.writer_given);
147 ret = -E_WRITE_SYNTAX;
148 for (i = 0; i < conf.writer_given; i++) {
150 g->writer_nodes[i].conf = check_writer_arg(
151 conf.writer_arg[i], &writer_num);
152 if (!g->writer_nodes[i].conf)
154 g->writer_nodes[i].writer = &writers[writer_num];
164 __noreturn static void print_help_and_die(void)
166 int d = conf.detailed_help_given;
167 const char **p = d? write_args_info_detailed_help
168 : write_args_info_help;
170 printf_or_die("%s\n\n", WRITE_CMDLINE_PARSER_PACKAGE "-"
171 WRITE_CMDLINE_PARSER_VERSION);
172 printf_or_die("%s\n\n", write_args_info_usage);
174 printf_or_die("%s\n", *p);
175 print_writer_helps(d);
180 * Para_write's main function.
182 * \param argc The usual argument counter.
183 * \param argv The usual argument vector.
185 * It registers the stdin task, the check_wav_task, the task for initial delay
186 * and all tasks for actually writing out the stream.
188 * \return \p EXIT_SUCCESS or EXIT_FAILURE
190 int main(int argc, char *argv[])
192 int ret = -E_WRITE_SYNTAX;
193 static struct sched s;
194 struct check_wav_task *cwt = &the_check_wav_task;
195 struct initial_delay_task *idt = &the_initial_delay_task;
197 init_supported_writers();
198 write_cmdline_parser(argc, argv, &conf);
199 HANDLE_VERSION_FLAG("write", conf);
200 if (conf.help_given || conf.detailed_help_given)
201 print_help_and_die();
206 stdin_set_defaults(&sit);
207 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
208 if (conf.bufsize_arg < 0)
210 if (conf.bufsize_arg >= INT_MAX / 1024)
212 sit.bufsize = conf.bufsize_arg * 1024;
213 sit.buf = para_malloc(sit.bufsize);
216 wng->loaded = &sit.loaded;
217 wng->input_error = &sit.task.error;
219 register_task(&sit.task);
222 cwt->loaded = &sit.loaded;
223 cwt->input_error = &sit.task.error;
224 sprintf(cwt->task.status, "check wav");
225 cwt->task.pre_select = check_wav_pre_select;
227 idt->task.pre_select = initial_delay_pre_select;
228 sprintf(idt->task.status, "initial_delay");
229 register_task(&idt->task);
231 s.default_timeout.tv_sec = 10;
232 s.default_timeout.tv_usec = 0;
237 PARA_ERROR_LOG("%s\n", para_strerror(-ret));