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 static 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(__a_unused 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;
70 cwt->sample_rate = 44100;
71 a = (unsigned char*)cwt->buf;
72 t->ret = -E_NO_WAV_HEADER;
73 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F')
75 cwt->channels = (unsigned) a[22];
76 cwt->sample_rate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
77 *cwt->loaded -= WAV_HEADER_LEN;
78 memmove(cwt->buf, cwt->buf + WAV_HEADER_LEN, *cwt->loaded);
79 t->ret = -E_WAV_HEADER_SUCCESS;
80 PARA_INFO_LOG("channels: %d, sample_rate: %d\n", cwt->channels, cwt->sample_rate);
83 static void initial_delay_pre_select(struct sched *s, struct task *t)
85 struct initial_delay_task *idt = t->private_data;
89 if (!idt->start_time.tv_sec && !idt->start_time.tv_usec)
91 t->ret = -E_DELAY_TIMEOUT;
92 if (tv_diff(&s->now, &idt->start_time, &diff) > 0)
95 if (tv_diff(&s->timeout , &diff, NULL) > 0)
99 void para_log(int ll, const char* fmt,...)
103 if (ll < conf.loglevel_arg)
106 vfprintf(stderr, fmt, argp);
110 static struct writer_node_group *check_args(void)
112 int i, ret = -E_WRITE_SYNTAX;
113 struct writer_node_group *wng = NULL;
115 if (conf.list_writers_given) {
118 char *tmp = make_message("%s%s%s",
125 fprintf(stderr, "%s\n", msg);
129 // if (conf.prebuffer_arg < 0 || conf.prebuffer_arg > 100)
131 if (conf.start_time_given) {
132 long unsigned sec, usec;
133 if (sscanf(conf.start_time_arg, "%lu:%lu",
136 idt.start_time.tv_sec = sec;
137 idt.start_time.tv_usec = usec;
139 if (!conf.writer_given) {
140 wng = setup_default_wng();
144 wng = wng_new(conf.writer_given);
145 ret = -E_WRITE_SYNTAX;
146 for (i = 0; i < conf.writer_given; i++) {
148 wng->writer_nodes[i].conf = check_writer_arg(
149 conf.writer_arg[i], &writer_num);
150 if (!wng->writer_nodes[i].conf)
152 wng->writer_nodes[i].writer = &writers[writer_num];
153 sprintf(wng->writer_nodes[i].task.status, "%s",
154 writer_names[writer_num]);
164 static void wng_event_handler(struct task *t)
166 struct writer_node_group *g = t->private_data;
168 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
175 static void idt_event_handler(struct task *t)
179 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
182 wng->loaded = &sit.loaded;
183 wng->input_eof = &sit.eof;
184 wng->task.event_handler = wng_event_handler;
187 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
192 static void cwt_event_handler(struct task *t)
194 if (t->ret != -E_NO_WAV_HEADER && t->ret != -E_WAV_HEADER_SUCCESS) {
195 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret));
198 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
200 // if (t->ret == -E_WAV_HEADER_SUCCESS) {
201 // conf.channels_arg = cwt.channels;
202 // conf.sample_rate_arg = cwt.sample_rate;
204 idt.task.pre_select = initial_delay_pre_select;
205 idt.task.private_data = &idt;
206 idt.task.event_handler = idt_event_handler;
207 sprintf(idt.task.status, "initial_delay");
208 register_task(&idt.task);
211 int main(int argc, char *argv[])
213 int ret = -E_WRITE_SYNTAX;
216 cmdline_parser(argc, argv, &conf);
217 init_supported_writers();
223 stdin_set_defaults(&sit);
224 if (conf.bufsize_given)
225 sit.bufsize = conf.bufsize_arg;
226 sit.buf = para_malloc(sit.bufsize),
227 register_task(&sit.task);
229 cwt.task.pre_select = check_wav_pre_select;
230 cwt.task.private_data = &cwt;
231 cwt.task.event_handler = cwt_event_handler;
233 cwt.loaded = &sit.loaded;
235 sprintf(cwt.task.status, "check wav");
236 register_task(&cwt.task);
238 s.default_timeout.tv_sec = 1;
239 s.default_timeout.tv_usec = 0;
244 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));