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;
111 if (tv_diff(now, &idt->start_time, &diff) > 0) {
112 t->error = -E_DELAY_TIMEOUT;
115 if (tv_diff(&s->timeout , &diff, NULL) > 0)
119 INIT_STDERR_LOGGING(conf.loglevel_arg)
121 static struct writer_node_group *check_args(void)
123 int i, ret = -E_WRITE_SYNTAX;
124 struct writer_node_group *g = NULL;
125 struct initial_delay_task *idt = &the_initial_delay_task;
127 if (conf.list_writers_given) {
130 char *tmp = make_message("%s%s%s",
137 fprintf(stderr, "%s\n", msg);
141 if (conf.start_time_given) {
142 long unsigned sec, usec;
143 if (sscanf(conf.start_time_arg, "%lu:%lu",
146 idt->start_time.tv_sec = sec;
147 idt->start_time.tv_usec = usec;
149 if (!conf.writer_given) {
150 g = setup_default_wng();
154 g = wng_new(conf.writer_given);
155 ret = -E_WRITE_SYNTAX;
156 for (i = 0; i < conf.writer_given; i++) {
158 g->writer_nodes[i].conf = check_writer_arg(
159 conf.writer_arg[i], &writer_num);
160 if (!g->writer_nodes[i].conf)
162 g->writer_nodes[i].writer = &writers[writer_num];
173 * Para_write's main function.
175 * \param argc The usual argument counter.
176 * \param argv The usual argument vector.
178 * It registers the stdin task, the check_wav_task, the task for initial delay
179 * and all tasks for actually writing out the stream.
181 * \return \p EXIT_SUCCESS or EXIT_FAILURE
183 int main(int argc, char *argv[])
185 int ret = -E_WRITE_SYNTAX;
186 static struct sched s;
187 struct check_wav_task *cwt = &the_check_wav_task;
188 struct initial_delay_task *idt = &the_initial_delay_task;
190 write_cmdline_parser(argc, argv, &conf);
191 HANDLE_VERSION_FLAG("write", conf);
192 init_supported_writers();
197 stdin_set_defaults(&sit);
198 if (conf.bufsize_given)
199 sit.bufsize = conf.bufsize_arg;
200 sit.buf = para_malloc(sit.bufsize);
203 wng->loaded = &sit.loaded;
204 wng->input_error = &sit.task.error;
206 register_task(&sit.task);
209 cwt->loaded = &sit.loaded;
210 cwt->input_error = &sit.task.error;
211 sprintf(cwt->task.status, "check wav");
212 cwt->task.pre_select = check_wav_pre_select;
213 register_task(&cwt->task);
215 idt->task.pre_select = initial_delay_pre_select;
216 sprintf(idt->task.status, "initial_delay");
217 register_task(&idt->task);
219 s.default_timeout.tv_sec = 10;
220 s.default_timeout.tv_usec = 0;
225 PARA_ERROR_LOG("%s\n", para_strerror(-ret));