2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file write.c Paraslash's standalone wav/raw player */
23 #include "write.cmdline.h"
28 #include "write_common.h"
35 * check if given buffer contains a valid wave header
37 struct check_wav_task {
38 /** the buffer to check */
40 /** number of bytes loaded in \a buf */
42 /** non-zero if end of file was reached */
44 /** number of channels specified in wav header given by \a buf */
46 /** samplerate specified in wav header given by \a buf */
48 /** the task structure for this task */
53 * delay writing until given time
55 struct initial_delay_task {
56 /** the time the first data should be written out */
57 struct timeval start_time;
58 /** the task structure for this task */
62 static struct write_args_info conf;
63 static struct stdin_task sit;
64 static struct check_wav_task cwt;
65 static struct initial_delay_task idt;
66 static struct writer_node_group *wng;
68 /** length of a standard wav header */
69 #define WAV_HEADER_LEN 44
72 * test if audio buffer contains a valid wave header
74 * \return If not, return -E_NO_WAV_HEADER, otherwise, return zero. If
75 * there is less than WAV_HEADER_LEN bytes awailable, return one.
77 static void check_wav_pre_select(__a_unused struct sched *s, struct task *t)
79 struct check_wav_task *wt = t->private_data;
82 if (*wt->loaded < WAV_HEADER_LEN) {
83 t->ret = *wt->eof? -E_PREMATURE_END : 1;
87 wt->samplerate = 44100;
88 a = (unsigned char*)wt->buf;
89 t->ret = -E_NO_WAV_HEADER;
90 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F')
92 wt->channels = (unsigned) a[22];
93 wt->samplerate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
94 *wt->loaded -= WAV_HEADER_LEN;
95 memmove(wt->buf, wt->buf + WAV_HEADER_LEN, *wt->loaded);
96 t->ret = -E_WAV_HEADER_SUCCESS;
97 PARA_INFO_LOG("channels: %d, sample rate: %d\n", wt->channels, wt->samplerate);
100 static void initial_delay_pre_select(struct sched *s, struct task *t)
102 struct initial_delay_task *dt = t->private_data;
105 t->ret = -E_NO_DELAY;
106 if (!dt->start_time.tv_sec && !dt->start_time.tv_usec)
108 t->ret = -E_DELAY_TIMEOUT;
109 if (tv_diff(now, &dt->start_time, &diff) > 0)
112 if (tv_diff(&s->timeout , &diff, NULL) > 0)
116 INIT_STDERR_LOGGING(conf.loglevel_arg)
118 static struct writer_node_group *check_args(void)
120 int i, ret = -E_WRITE_SYNTAX;
121 struct writer_node_group *g = NULL;
123 if (conf.list_writers_given) {
126 char *tmp = make_message("%s%s%s",
133 fprintf(stderr, "%s\n", msg);
137 if (conf.start_time_given) {
138 long unsigned sec, usec;
139 if (sscanf(conf.start_time_arg, "%lu:%lu",
142 idt.start_time.tv_sec = sec;
143 idt.start_time.tv_usec = usec;
145 if (!conf.writer_given) {
146 g = setup_default_wng();
150 g = wng_new(conf.writer_given);
151 ret = -E_WRITE_SYNTAX;
152 for (i = 0; i < conf.writer_given; i++) {
154 g->writer_nodes[i].conf = check_writer_arg(
155 conf.writer_arg[i], &writer_num);
156 if (!g->writer_nodes[i].conf)
158 g->writer_nodes[i].writer = &writers[writer_num];
168 static void wng_event_handler(struct task *t)
170 struct writer_node_group *g = t->private_data;
172 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
178 static void idt_event_handler(struct task *t)
182 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
185 wng->loaded = &sit.loaded;
186 wng->input_eof = &sit.eof;
187 wng->task.event_handler = wng_event_handler;
188 wng->channels = &cwt.channels;
189 wng->samplerate = &cwt.samplerate;
192 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
197 static void cwt_event_handler(struct task *t)
199 if (t->ret != -E_NO_WAV_HEADER && t->ret != -E_WAV_HEADER_SUCCESS) {
200 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret));
203 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
205 // if (t->ret == -E_WAV_HEADER_SUCCESS) {
206 // conf.channels_arg = cwt.channels;
207 // conf.sample_rate_arg = cwt.sample_rate;
209 idt.task.pre_select = initial_delay_pre_select;
210 idt.task.private_data = &idt;
211 idt.task.event_handler = idt_event_handler;
212 sprintf(idt.task.status, "initial_delay");
213 register_task(&idt.task);
217 * para_write's main function
219 * It registers the stdin task, the check_wav_task, the task for initial delay
220 * and all tasks for actually writing out the stream.
223 int main(int argc, char *argv[])
225 int ret = -E_WRITE_SYNTAX;
228 write_cmdline_parser(argc, argv, &conf);
229 init_supported_writers();
234 stdin_set_defaults(&sit);
235 if (conf.bufsize_given)
236 sit.bufsize = conf.bufsize_arg;
237 sit.buf = para_malloc(sit.bufsize),
238 register_task(&sit.task);
240 cwt.task.pre_select = check_wav_pre_select;
241 cwt.task.private_data = &cwt;
242 cwt.task.event_handler = cwt_event_handler;
244 cwt.loaded = &sit.loaded;
246 sprintf(cwt.task.status, "check wav");
247 register_task(&cwt.task);
249 s.default_timeout.tv_sec = 1;
250 s.default_timeout.tv_usec = 0;
255 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));