2 * Copyright (C) 2005-2008 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"
25 /** Check if given buffer contains a valid wave header. */
26 struct check_wav_task {
27 /** The buffer to check. */
29 /** Number of bytes loaded in \a buf. */
31 /** Non-zero if an error occurred or end of file was reached. */
33 /** Number of channels specified in wav header given by \a buf. */
35 /** Sample rate specified in wav header given by \a buf. */
40 /** Delay writing until given time. */
41 struct initial_delay_task {
42 /** The time the first data should be written out. */
43 struct timeval start_time;
44 /** The task structure for this task. */
48 static struct write_args_info conf;
50 static struct stdin_task sit;
52 static struct check_wav_task the_check_wav_task;
53 static struct initial_delay_task the_initial_delay_task;
55 static struct writer_node_group *wng;
57 /** Length of a standard wav header. */
58 #define WAV_HEADER_LEN 44
61 * Test if audio buffer contains a valid wave header.
63 * \return If not, return -E_NO_WAV_HEADER, otherwise, return zero. If
64 * there is less than WAV_HEADER_LEN bytes available, return one.
66 static void check_wav_pre_select(__a_unused struct sched *s, struct task *t)
68 struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
72 if (*cwt->loaded < WAV_HEADER_LEN) {
73 if (*cwt->input_error < 0)
74 t->error = *cwt->input_error;
78 cwt->samplerate = 44100;
79 a = (unsigned char*)cwt->buf;
80 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F') {
81 PARA_NOTICE_LOG("wav header not found\n");
82 t->error = -E_NO_WAV_HEADER;
85 cwt->channels = (unsigned) a[22];
86 cwt->samplerate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
87 *cwt->loaded -= WAV_HEADER_LEN;
88 memmove(cwt->buf, cwt->buf + WAV_HEADER_LEN, *cwt->loaded);
89 t->error = -E_WAV_HEADER_SUCCESS;
90 PARA_INFO_LOG("channels: %d, sample rate: %d\n", cwt->channels, cwt->samplerate);
92 wng->channels = &cwt->channels;
93 wng->samplerate = &cwt->samplerate;
97 s->timeout.tv_sec = 0;
98 s->timeout.tv_usec = 1;
101 static void initial_delay_pre_select(struct sched *s, struct task *t)
103 struct initial_delay_task *idt = container_of(t, struct initial_delay_task, task);
106 if (!idt->start_time.tv_sec && !idt->start_time.tv_usec) {
107 t->error = -E_NO_DELAY;
110 if (tv_diff(now, &idt->start_time, &diff) > 0) {
111 t->error = -E_DELAY_TIMEOUT;
114 if (tv_diff(&s->timeout , &diff, NULL) > 0)
118 INIT_STDERR_LOGGING(conf.loglevel_arg)
120 static struct writer_node_group *check_args(void)
122 int i, ret = -E_WRITE_SYNTAX;
123 struct writer_node_group *g = NULL;
124 struct initial_delay_task *idt = &the_initial_delay_task;
126 if (conf.list_writers_given) {
129 char *tmp = make_message("%s%s%s",
136 fprintf(stderr, "%s\n", msg);
140 if (conf.start_time_given) {
141 long unsigned sec, usec;
142 if (sscanf(conf.start_time_arg, "%lu:%lu",
145 idt->start_time.tv_sec = sec;
146 idt->start_time.tv_usec = usec;
148 if (!conf.writer_given) {
149 g = setup_default_wng();
153 g = wng_new(conf.writer_given);
154 ret = -E_WRITE_SYNTAX;
155 for (i = 0; i < conf.writer_given; i++) {
157 g->writer_nodes[i].conf = check_writer_arg(
158 conf.writer_arg[i], &writer_num);
159 if (!g->writer_nodes[i].conf)
161 g->writer_nodes[i].writer = &writers[writer_num];
172 * Para_write's main function.
174 * \param argc The usual argument counter.
175 * \param argv The usual argument vector.
177 * It registers the stdin task, the check_wav_task, the task for initial delay
178 * and all tasks for actually writing out the stream.
180 * \return \p EXIT_SUCCESS or EXIT_FAILURE
182 int main(int argc, char *argv[])
184 int ret = -E_WRITE_SYNTAX;
185 static struct sched s;
186 struct check_wav_task *cwt = &the_check_wav_task;
187 struct initial_delay_task *idt = &the_initial_delay_task;
189 write_cmdline_parser(argc, argv, &conf);
190 HANDLE_VERSION_FLAG("write", conf);
191 init_supported_writers();
196 stdin_set_defaults(&sit);
197 if (conf.bufsize_given)
198 sit.bufsize = conf.bufsize_arg;
199 sit.buf = para_malloc(sit.bufsize);
202 wng->loaded = &sit.loaded;
203 wng->input_error = &sit.task.error;
205 register_task(&sit.task);
208 cwt->loaded = &sit.loaded;
209 cwt->input_error = &sit.task.error;
210 sprintf(cwt->task.status, "check wav");
211 cwt->task.pre_select = check_wav_pre_select;
212 register_task(&cwt->task);
214 idt->task.pre_select = initial_delay_pre_select;
215 sprintf(idt->task.status, "initial_delay");
216 register_task(&idt->task);
218 s.default_timeout.tv_sec = 10;
219 s.default_timeout.tv_usec = 0;
224 PARA_ERROR_LOG("%s\n", para_strerror(-ret));