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.
21 #include "write.cmdline.h"
26 #include "write_common.h"
32 struct check_wav_task {
41 struct initial_delay_task {
42 struct timeval start_time;
46 struct gengetopt_args_info conf;
47 struct stdin_task sit;
48 struct check_wav_task cwt;
49 struct initial_delay_task idt;
50 static struct writer_node_group *wng;
52 #define WAV_HEADER_LEN 44
55 * test if audio buffer contains a valid wave header
57 * \return If not, return -E_NO_WAV_HEADER, otherwise, return zero. If
58 * there is less than WAV_HEADER_LEN bytes awailable, return one.
60 static void check_wav_pre_select(struct sched *s, struct task *t)
62 struct check_wav_task *cwt = t->private_data;
65 if (*cwt->loaded < WAV_HEADER_LEN) {
66 t->ret = *cwt->eof? -E_PREMATURE_END : 1;
69 a = (unsigned char*)cwt->buf;
70 t->ret = -E_NO_WAV_HEADER;
71 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F')
73 cwt->channels = (unsigned) a[22];
74 cwt->sample_rate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
75 *cwt->loaded -= WAV_HEADER_LEN;
76 memmove(cwt->buf, cwt->buf + WAV_HEADER_LEN, *cwt->loaded);
78 PARA_INFO_LOG("channels: %d, sample_rate: %d\n", cwt->channels, cwt->sample_rate);
81 static void initial_delay_pre_select(struct sched *s, struct task *t)
83 struct initial_delay_task *idt = t->private_data;
86 PARA_ERROR_LOG("task %p, ret: %d\n", t, t->ret);
88 if (!idt->start_time.tv_sec && !idt->start_time.tv_usec)
90 t->ret = 0; /* timeout */
91 if (tv_diff(&s->now, &idt->start_time, &diff) > 0)
94 if (tv_diff(&s->timeout , &diff, NULL) > 0)
98 void para_log(int ll, const char* fmt,...)
102 if (ll < conf.loglevel_arg)
105 vfprintf(stderr, fmt, argp);
109 static struct writer_node_group *check_args(void)
111 int i, ret = -E_WRITE_SYNTAX;
112 struct writer_node_group *wng = NULL;
114 if (conf.list_writers_given) {
117 char *tmp = make_message("%s%s%s",
124 fprintf(stderr, "%s\n", msg);
128 if (conf.prebuffer_arg < 0 || conf.prebuffer_arg > 100)
130 if (conf.start_time_given) {
131 long unsigned sec, usec;
132 if (sscanf(conf.start_time_arg, "%lu:%lu",
135 idt.start_time.tv_sec = sec;
136 idt.start_time.tv_usec = usec;
138 if (!conf.writer_given) {
139 wng = setup_default_wng();
143 wng = wng_new(conf.writer_given);
144 for (i = 0; i < conf.writer_given; i++) {
145 ret = check_writer_arg(conf.writer_arg[i]);
148 wng->writer_nodes[i].writer = &writers[ret];
159 static void idt_error_handler(struct task *t)
161 PARA_ERROR_LOG("task %p, ret: %d\n", t, t->ret);
165 wng->loaded = &sit.loaded;
169 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
174 static void cwt_error_handler(struct task *t)
176 PARA_ERROR_LOG("task %p, ret: %d\n", t, t->ret);
178 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret));
179 if (t->ret != -E_NO_WAV_HEADER && t->ret != -E_PRE_EOF)
181 if (t->ret == -E_PRE_EOF) {
182 conf.channels_arg = cwt.channels;
183 conf.sample_rate_arg = cwt.sample_rate;
187 idt.task.pre_select = initial_delay_pre_select;
188 idt.task.private_data = &idt;
189 idt.task.error_handler = idt_error_handler;
190 idt.task.flags = PRE_EOF_IS_ERROR;
191 register_task(&idt.task);
194 static void stdin_error_handler(struct task *t)
197 PARA_INFO_LOG("task %p, ret: %d\n", t, t->ret);
199 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret));
202 int main(int argc, char *argv[])
204 int ret = -E_WRITE_SYNTAX;
207 cmdline_parser(argc, argv, &conf);
211 init_supported_writers();
214 sit.bufsize = 16 * 1024,
215 sit.buf = para_malloc(16 * 1024),
217 sit.task.pre_select = stdin_pre_select;
218 sit.task.post_select = stdin_post_select;
219 sit.task.error_handler = stdin_error_handler;
220 sit.task.flags = POST_EOF_IS_ERROR;
221 sit.task.private_data = &sit;
222 register_task(&sit.task);
224 cwt.task.pre_select = check_wav_pre_select;
225 cwt.task.private_data = &cwt;
226 cwt.task.error_handler = cwt_error_handler;
228 cwt.loaded = &sit.loaded;
230 cwt.task.flags = PRE_EOF_IS_ERROR;
231 register_task(&cwt.task);
233 s.default_timeout.tv_sec = 1;
234 s.default_timeout.tv_usec = 0;
239 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));