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"
19 #include "write_common.h"
25 /** Check if given buffer contains a valid wave header. */
26 struct check_wav_task {
27 /** The buffer to check. */
29 /** Number of bytes loaded in \a buf. */
31 /** Non-zero if an error occurred or end of file was reached. */
33 /** Number of channels specified in wav header given by \a buf. */
35 /** Sample rate specified in wav header given by \a buf. */
37 /** The task structure used by the scheduler. */
41 /** Delay writing until given time. */
42 struct initial_delay_task {
43 /** The time the first data should be written out. */
44 struct timeval start_time;
45 /** The task structure for this task. */
49 static struct write_args_info conf;
51 static struct stdin_task sit;
53 static struct check_wav_task the_check_wav_task;
54 static struct initial_delay_task the_initial_delay_task;
56 static struct writer_node_group *wng;
58 /** Length of a standard wav header. */
59 #define WAV_HEADER_LEN 44
62 * Test if audio buffer contains a valid wave header.
64 * \return If not, return -E_NO_WAV_HEADER, otherwise, return zero. If
65 * there is less than WAV_HEADER_LEN bytes available, return one.
67 static void check_wav_pre_select(__a_unused struct sched *s, struct task *t)
69 struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
73 if (*cwt->loaded < WAV_HEADER_LEN) {
74 if (*cwt->input_error < 0)
75 t->error = *cwt->input_error;
79 cwt->samplerate = 44100;
80 a = (unsigned char*)cwt->buf;
81 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F') {
82 PARA_NOTICE_LOG("wav header not found\n");
83 t->error = -E_NO_WAV_HEADER;
86 cwt->channels = (unsigned) a[22];
87 cwt->samplerate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
88 *cwt->loaded -= WAV_HEADER_LEN;
89 memmove(cwt->buf, cwt->buf + WAV_HEADER_LEN, *cwt->loaded);
90 t->error = -E_WAV_HEADER_SUCCESS;
91 PARA_INFO_LOG("channels: %d, sample rate: %d\n", cwt->channels, cwt->samplerate);
93 wng->channels = &cwt->channels;
94 wng->samplerate = &cwt->samplerate;
98 s->timeout.tv_sec = 0;
99 s->timeout.tv_usec = 1;
102 static void initial_delay_pre_select(struct sched *s, struct task *t)
104 struct initial_delay_task *idt = container_of(t, struct initial_delay_task, task);
107 if (!idt->start_time.tv_sec && !idt->start_time.tv_usec) {
108 t->error = -E_NO_DELAY;
109 goto register_check_wav;
111 if (tv_diff(now, &idt->start_time, &diff) > 0) {
112 t->error = -E_DELAY_TIMEOUT;
113 goto register_check_wav;
115 if (tv_diff(&s->timeout , &diff, NULL) > 0)
119 register_task(&the_check_wav_task.task);
120 s->timeout.tv_sec = 0;
121 s->timeout.tv_usec = 1;
124 INIT_STDERR_LOGGING(conf.loglevel_arg)
126 static struct writer_node_group *check_args(void)
128 int i, ret = -E_WRITE_SYNTAX;
129 struct writer_node_group *g = NULL;
130 struct initial_delay_task *idt = &the_initial_delay_task;
132 if (conf.list_writers_given) {
135 char *tmp = make_message("%s%s%s",
142 fprintf(stderr, "%s\n", msg);
146 if (conf.start_time_given) {
147 long unsigned sec, usec;
148 if (sscanf(conf.start_time_arg, "%lu:%lu",
151 idt->start_time.tv_sec = sec;
152 idt->start_time.tv_usec = usec;
154 if (!conf.writer_given) {
155 g = setup_default_wng();
159 g = wng_new(conf.writer_given);
160 ret = -E_WRITE_SYNTAX;
161 for (i = 0; i < conf.writer_given; i++) {
163 g->writer_nodes[i].conf = check_writer_arg(
164 conf.writer_arg[i], &writer_num);
165 if (!g->writer_nodes[i].conf)
167 g->writer_nodes[i].writer = &writers[writer_num];
178 * Para_write's main function.
180 * \param argc The usual argument counter.
181 * \param argv The usual argument vector.
183 * It registers the stdin task, the check_wav_task, the task for initial delay
184 * and all tasks for actually writing out the stream.
186 * \return \p EXIT_SUCCESS or EXIT_FAILURE
188 int main(int argc, char *argv[])
190 int ret = -E_WRITE_SYNTAX;
191 static struct sched s;
192 struct check_wav_task *cwt = &the_check_wav_task;
193 struct initial_delay_task *idt = &the_initial_delay_task;
195 write_cmdline_parser(argc, argv, &conf);
196 HANDLE_VERSION_FLAG("write", conf);
197 init_supported_writers();
202 stdin_set_defaults(&sit);
203 if (conf.bufsize_given)
204 sit.bufsize = conf.bufsize_arg;
205 sit.buf = para_malloc(sit.bufsize);
208 wng->loaded = &sit.loaded;
209 wng->input_error = &sit.task.error;
211 register_task(&sit.task);
214 cwt->loaded = &sit.loaded;
215 cwt->input_error = &sit.task.error;
216 sprintf(cwt->task.status, "check wav");
217 cwt->task.pre_select = check_wav_pre_select;
219 idt->task.pre_select = initial_delay_pre_select;
220 sprintf(idt->task.status, "initial_delay");
221 register_task(&idt->task);
223 s.default_timeout.tv_sec = 10;
224 s.default_timeout.tv_usec = 0;
229 PARA_ERROR_LOG("%s\n", para_strerror(-ret));