2 * Copyright (C) 2005-2006 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)
117 * all log messages are written to stderr
119 * \param ll: loglevel
120 * \param fmt: format string
122 void para_log(int ll, const char* fmt,...)
126 if (ll < conf.loglevel_arg)
129 vfprintf(stderr, fmt, argp);
133 static struct writer_node_group *check_args(void)
135 int i, ret = -E_WRITE_SYNTAX;
136 struct writer_node_group *g = NULL;
138 if (conf.list_writers_given) {
141 char *tmp = make_message("%s%s%s",
148 fprintf(stderr, "%s\n", msg);
152 if (conf.start_time_given) {
153 long unsigned sec, usec;
154 if (sscanf(conf.start_time_arg, "%lu:%lu",
157 idt.start_time.tv_sec = sec;
158 idt.start_time.tv_usec = usec;
160 if (!conf.writer_given) {
161 g = setup_default_wng();
165 g = wng_new(conf.writer_given);
166 ret = -E_WRITE_SYNTAX;
167 for (i = 0; i < conf.writer_given; i++) {
169 g->writer_nodes[i].conf = check_writer_arg(
170 conf.writer_arg[i], &writer_num);
171 if (!g->writer_nodes[i].conf)
173 g->writer_nodes[i].writer = &writers[writer_num];
183 static void wng_event_handler(struct task *t)
185 struct writer_node_group *g = t->private_data;
187 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
193 static void idt_event_handler(struct task *t)
197 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
200 wng->loaded = &sit.loaded;
201 wng->input_eof = &sit.eof;
202 wng->task.event_handler = wng_event_handler;
203 wng->channels = &cwt.channels;
204 wng->samplerate = &cwt.samplerate;
207 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
212 static void cwt_event_handler(struct task *t)
214 if (t->ret != -E_NO_WAV_HEADER && t->ret != -E_WAV_HEADER_SUCCESS) {
215 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret));
218 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
220 // if (t->ret == -E_WAV_HEADER_SUCCESS) {
221 // conf.channels_arg = cwt.channels;
222 // conf.sample_rate_arg = cwt.sample_rate;
224 idt.task.pre_select = initial_delay_pre_select;
225 idt.task.private_data = &idt;
226 idt.task.event_handler = idt_event_handler;
227 sprintf(idt.task.status, "initial_delay");
228 register_task(&idt.task);
232 * para_write's main function
234 * It registers the stdin task, the check_wav_task, the task for initial delay
235 * and all tasks for actually writing out the stream.
238 int main(int argc, char *argv[])
240 int ret = -E_WRITE_SYNTAX;
243 write_cmdline_parser(argc, argv, &conf);
244 init_supported_writers();
249 stdin_set_defaults(&sit);
250 if (conf.bufsize_given)
251 sit.bufsize = conf.bufsize_arg;
252 sit.buf = para_malloc(sit.bufsize),
253 register_task(&sit.task);
255 cwt.task.pre_select = check_wav_pre_select;
256 cwt.task.private_data = &cwt;
257 cwt.task.event_handler = cwt_event_handler;
259 cwt.loaded = &sit.loaded;
261 sprintf(cwt.task.status, "check wav");
262 register_task(&cwt.task);
264 s.default_timeout.tv_sec = 1;
265 s.default_timeout.tv_usec = 0;
270 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));