2 * Copyright (C) 2005-2007 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"
28 * check if given buffer contains a valid wave header
30 struct check_wav_task {
31 /** the buffer to check */
33 /** number of bytes loaded in \a buf */
35 /** non-zero if end of file was reached */
37 /** number of channels specified in wav header given by \a buf */
39 /** samplerate specified in wav header given by \a buf */
41 /** the task structure for this task */
46 * delay writing until given time
48 struct initial_delay_task {
49 /** the time the first data should be written out */
50 struct timeval start_time;
51 /** the task structure for this task */
55 static struct write_args_info conf;
56 static struct stdin_task sit;
57 static struct check_wav_task cwt;
58 static struct initial_delay_task idt;
59 static struct writer_node_group *wng;
61 /** length of a standard wav header */
62 #define WAV_HEADER_LEN 44
65 * test if audio buffer contains a valid wave header
67 * \return If not, return -E_NO_WAV_HEADER, otherwise, return zero. If
68 * there is less than WAV_HEADER_LEN bytes awailable, return one.
70 static void check_wav_pre_select(__a_unused struct sched *s, struct task *t)
72 struct check_wav_task *wt = t->private_data;
75 if (*wt->loaded < WAV_HEADER_LEN) {
76 t->ret = *wt->eof? -E_PREMATURE_END : 1;
80 wt->samplerate = 44100;
81 a = (unsigned char*)wt->buf;
82 t->ret = -E_NO_WAV_HEADER;
83 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F')
85 wt->channels = (unsigned) a[22];
86 wt->samplerate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
87 *wt->loaded -= WAV_HEADER_LEN;
88 memmove(wt->buf, wt->buf + WAV_HEADER_LEN, *wt->loaded);
89 t->ret = -E_WAV_HEADER_SUCCESS;
90 PARA_INFO_LOG("channels: %d, sample rate: %d\n", wt->channels, wt->samplerate);
93 static void initial_delay_pre_select(struct sched *s, struct task *t)
95 struct initial_delay_task *dt = t->private_data;
99 if (!dt->start_time.tv_sec && !dt->start_time.tv_usec)
101 t->ret = -E_DELAY_TIMEOUT;
102 if (tv_diff(now, &dt->start_time, &diff) > 0)
105 if (tv_diff(&s->timeout , &diff, NULL) > 0)
109 INIT_STDERR_LOGGING(conf.loglevel_arg)
111 static struct writer_node_group *check_args(void)
113 int i, ret = -E_WRITE_SYNTAX;
114 struct writer_node_group *g = NULL;
116 if (conf.list_writers_given) {
119 char *tmp = make_message("%s%s%s",
126 fprintf(stderr, "%s\n", msg);
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 g = setup_default_wng();
143 g = wng_new(conf.writer_given);
144 ret = -E_WRITE_SYNTAX;
145 for (i = 0; i < conf.writer_given; i++) {
147 g->writer_nodes[i].conf = check_writer_arg(
148 conf.writer_arg[i], &writer_num);
149 if (!g->writer_nodes[i].conf)
151 g->writer_nodes[i].writer = &writers[writer_num];
161 static void wng_event_handler(struct task *t)
163 struct writer_node_group *g = t->private_data;
165 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
171 static void idt_event_handler(struct task *t)
175 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
178 wng->loaded = &sit.loaded;
179 wng->input_eof = &sit.eof;
180 wng->task.event_handler = wng_event_handler;
181 wng->channels = &cwt.channels;
182 wng->samplerate = &cwt.samplerate;
185 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
190 static void cwt_event_handler(struct task *t)
192 if (t->ret != -E_NO_WAV_HEADER && t->ret != -E_WAV_HEADER_SUCCESS) {
193 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret));
196 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
198 // if (t->ret == -E_WAV_HEADER_SUCCESS) {
199 // conf.channels_arg = cwt.channels;
200 // conf.sample_rate_arg = cwt.sample_rate;
202 idt.task.pre_select = initial_delay_pre_select;
203 idt.task.private_data = &idt;
204 idt.task.event_handler = idt_event_handler;
205 sprintf(idt.task.status, "initial_delay");
206 register_task(&idt.task);
210 * para_write's main function
212 * \param argc the usual argument counter
213 * \param argv the usual argument vector
215 * It registers the stdin task, the check_wav_task, the task for initial delay
216 * and all tasks for actually writing out the stream.
218 * \return \p EXIT_SUCCESS or EXIT_FAILURE
220 int main(int argc, char *argv[])
222 int ret = -E_WRITE_SYNTAX;
225 write_cmdline_parser(argc, argv, &conf);
226 HANDLE_VERSION_FLAG("write", conf);
227 init_supported_writers();
232 stdin_set_defaults(&sit);
233 if (conf.bufsize_given)
234 sit.bufsize = conf.bufsize_arg;
235 sit.buf = para_malloc(sit.bufsize),
236 register_task(&sit.task);
238 cwt.task.pre_select = check_wav_pre_select;
239 cwt.task.private_data = &cwt;
240 cwt.task.event_handler = cwt_event_handler;
242 cwt.loaded = &sit.loaded;
244 sprintf(cwt.task.status, "check wav");
245 register_task(&cwt.task);
247 s.default_timeout.tv_sec = 1;
248 s.default_timeout.tv_usec = 0;
253 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));