2 * Copyright (C) 2005-2009 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. */
10 #include <sys/types.h>
15 #include "write.cmdline.h"
21 #include "write_common.h"
27 /** Check if given buffer contains a valid wave header. */
28 struct check_wav_task {
29 /** The buffer to check. */
31 /** Number of bytes loaded in \a buf. */
33 /** Non-zero if an error occurred or end of file was reached. */
35 /** Number of channels specified in wav header given by \a buf. */
37 /** Sample rate specified in wav header given by \a buf. */
39 /** The task structure used by the scheduler. */
43 /** Delay writing until given time. */
44 struct initial_delay_task {
45 /** The time the first data should be written out. */
46 struct timeval start_time;
47 /** The task structure for this task. */
51 static struct write_args_info conf;
53 static struct stdin_task sit;
55 static struct check_wav_task the_check_wav_task;
56 static struct initial_delay_task the_initial_delay_task;
58 static struct writer_node_group *wng;
60 /** Length of a standard wav header. */
61 #define WAV_HEADER_LEN 44
64 * Test if audio buffer contains a valid wave header.
66 * \return If not, return -E_NO_WAV_HEADER, otherwise, return zero. If
67 * there is less than WAV_HEADER_LEN bytes available, return one.
69 static void check_wav_pre_select(__a_unused struct sched *s, struct task *t)
71 struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
75 if (*cwt->loaded < WAV_HEADER_LEN) {
76 if (*cwt->input_error < 0)
77 t->error = *cwt->input_error;
81 cwt->samplerate = 44100;
82 a = (unsigned char*)cwt->buf;
83 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F') {
84 PARA_NOTICE_LOG("wav header not found\n");
85 t->error = -E_NO_WAV_HEADER;
88 cwt->channels = (unsigned) a[22];
89 cwt->samplerate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
90 *cwt->loaded -= WAV_HEADER_LEN;
91 memmove(cwt->buf, cwt->buf + WAV_HEADER_LEN, *cwt->loaded);
92 t->error = -E_WAV_HEADER_SUCCESS;
93 PARA_INFO_LOG("channels: %d, sample rate: %d\n", cwt->channels, cwt->samplerate);
95 wng->channels = &cwt->channels;
96 wng->samplerate = &cwt->samplerate;
100 s->timeout.tv_sec = 0;
101 s->timeout.tv_usec = 1;
104 static void initial_delay_pre_select(struct sched *s, struct task *t)
106 struct initial_delay_task *idt = container_of(t, struct initial_delay_task, task);
109 if (!idt->start_time.tv_sec && !idt->start_time.tv_usec) {
110 t->error = -E_NO_DELAY;
111 goto register_check_wav;
113 if (tv_diff(now, &idt->start_time, &diff) > 0) {
114 t->error = -E_DELAY_TIMEOUT;
115 goto register_check_wav;
117 if (tv_diff(&s->timeout , &diff, NULL) > 0)
121 register_task(&the_check_wav_task.task);
122 s->timeout.tv_sec = 0;
123 s->timeout.tv_usec = 1;
127 INIT_STDERR_LOGGING(loglevel)
129 static struct writer_node_group *check_args(void)
131 int i, ret = -E_WRITE_SYNTAX;
132 struct writer_node_group *g = NULL;
133 struct initial_delay_task *idt = &the_initial_delay_task;
135 loglevel = get_loglevel_by_name(conf.loglevel_arg);
136 if (conf.start_time_given) {
137 long unsigned sec, usec;
138 if (sscanf(conf.start_time_arg, "%lu:%lu",
141 idt->start_time.tv_sec = sec;
142 idt->start_time.tv_usec = usec;
144 if (!conf.writer_given) {
145 g = setup_default_wng();
149 g = wng_new(conf.writer_given);
150 ret = -E_WRITE_SYNTAX;
151 for (i = 0; i < conf.writer_given; i++) {
153 g->writer_nodes[i].conf = check_writer_arg(
154 conf.writer_arg[i], &writer_num);
155 if (!g->writer_nodes[i].conf)
157 g->writer_nodes[i].writer_num = writer_num;
167 __noreturn static void print_help_and_die(void)
169 int d = conf.detailed_help_given;
170 const char **p = d? write_args_info_detailed_help
171 : write_args_info_help;
173 printf_or_die("%s\n\n", WRITE_CMDLINE_PARSER_PACKAGE "-"
174 WRITE_CMDLINE_PARSER_VERSION);
175 printf_or_die("%s\n\n", write_args_info_usage);
177 printf_or_die("%s\n", *p);
178 print_writer_helps(d);
183 * Para_write's main function.
185 * \param argc The usual argument counter.
186 * \param argv The usual argument vector.
188 * It registers the stdin task, the check_wav_task, the task for initial delay
189 * and all tasks for actually writing out the stream.
191 * \return \p EXIT_SUCCESS or EXIT_FAILURE
193 int main(int argc, char *argv[])
195 int ret = -E_WRITE_SYNTAX;
196 static struct sched s;
197 struct check_wav_task *cwt = &the_check_wav_task;
198 struct initial_delay_task *idt = &the_initial_delay_task;
201 write_cmdline_parser(argc, argv, &conf);
202 HANDLE_VERSION_FLAG("write", conf);
203 if (conf.help_given || conf.detailed_help_given)
204 print_help_and_die();
209 stdin_set_defaults(&sit);
210 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
211 if (conf.bufsize_arg < 0)
213 if (conf.bufsize_arg >= INT_MAX / 1024)
215 sit.bufsize = conf.bufsize_arg * 1024;
216 sit.buf = para_malloc(sit.bufsize);
218 wng->bufp = &sit.buf;
219 wng->loaded = &sit.loaded;
220 wng->input_error = &sit.task.error;
222 register_task(&sit.task);
225 cwt->loaded = &sit.loaded;
226 cwt->input_error = &sit.task.error;
227 sprintf(cwt->task.status, "check wav");
228 cwt->task.pre_select = check_wav_pre_select;
230 idt->task.pre_select = initial_delay_pre_select;
231 sprintf(idt->task.status, "initial_delay");
232 register_task(&idt->task);
234 s.default_timeout.tv_sec = 10;
235 s.default_timeout.tv_usec = 0;
240 PARA_ERROR_LOG("%s\n", para_strerror(-ret));