21a0dad9253e66877ada6da5814062764d1269fb
[paraslash.git] / write.c
1 /*
2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file write.c Paraslash's standalone wav/raw player. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11 #include <dirent.h>
12 #include <stdbool.h>
13
14 #include "para.h"
15 #include "string.h"
16 #include "write.cmdline.h"
17 #include "list.h"
18 #include "sched.h"
19 #include "ggo.h"
20 #include "stdin.h"
21 #include "write.h"
22 #include "write_common.h"
23 #include "fd.h"
24 #include "error.h"
25 #include "buffer_tree.h"
26
27 INIT_WRITE_ERRLISTS;
28
29 /** Check if given buffer contains a valid wave header. */
30 struct check_wav_task {
31 /** The buffer to check. */
32 char *buf;
33 /** Number of bytes loaded in \a buf. */
34 size_t *loaded;
35 /** Non-zero if an error occurred or end of file was reached. */
36 int *input_error;
37 /** Number of channels specified in wav header given by \a buf. */
38 unsigned channels;
39 /** Sample rate specified in wav header given by \a buf. */
40 unsigned samplerate;
41 /** The task structure used by the scheduler. */
42 struct task task;
43 };
44
45 /** Delay writing until given time. */
46 struct initial_delay_task {
47 /** The time the first data should be written out. */
48 struct timeval start_time;
49 /** The task structure for this task. */
50 struct task task;
51 };
52
53 static struct write_args_info conf;
54
55 static struct stdin_task sit;
56
57 static struct check_wav_task the_check_wav_task;
58 static struct initial_delay_task the_initial_delay_task;
59
60 static struct writer_node_group *wng;
61
62 /** Length of a standard wav header. */
63 #define WAV_HEADER_LEN 44
64
65 /**
66 * Test if audio buffer contains a valid wave header.
67 *
68 * \return If not, return -E_NO_WAV_HEADER, otherwise, return zero. If
69 * there is less than WAV_HEADER_LEN bytes available, return one.
70 */
71 static void check_wav_pre_select(__a_unused struct sched *s, struct task *t)
72 {
73 struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
74 unsigned char *a;
75 int ret;
76
77 if (*cwt->loaded < WAV_HEADER_LEN) {
78 if (*cwt->input_error < 0)
79 t->error = *cwt->input_error;
80 return;
81 }
82 cwt->channels = 2;
83 cwt->samplerate = 44100;
84 a = (unsigned char*)cwt->buf;
85 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F') {
86 PARA_NOTICE_LOG("wav header not found\n");
87 t->error = -E_NO_WAV_HEADER;
88 goto out;
89 }
90 cwt->channels = (unsigned) a[22];
91 cwt->samplerate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
92 *cwt->loaded -= WAV_HEADER_LEN;
93 memmove(cwt->buf, cwt->buf + WAV_HEADER_LEN, *cwt->loaded);
94 t->error = -E_WAV_HEADER_SUCCESS;
95 PARA_INFO_LOG("channels: %d, sample rate: %d\n", cwt->channels, cwt->samplerate);
96 out:
97 wng->channels = &cwt->channels;
98 wng->samplerate = &cwt->samplerate;
99 ret = wng_open(wng);
100 if (ret < 0)
101 t->error = ret;
102 s->timeout.tv_sec = 0;
103 s->timeout.tv_usec = 1;
104 }
105
106 static void initial_delay_pre_select(struct sched *s, struct task *t)
107 {
108 struct initial_delay_task *idt = container_of(t, struct initial_delay_task, task);
109 struct timeval diff;
110
111 if (!idt->start_time.tv_sec && !idt->start_time.tv_usec) {
112 t->error = -E_NO_DELAY;
113 goto register_check_wav;
114 }
115 if (tv_diff(now, &idt->start_time, &diff) > 0) {
116 t->error = -E_DELAY_TIMEOUT;
117 goto register_check_wav;
118 }
119 if (tv_diff(&s->timeout , &diff, NULL) > 0)
120 s->timeout = diff;
121 return;
122 register_check_wav:
123 register_task(&the_check_wav_task.task);
124 s->timeout.tv_sec = 0;
125 s->timeout.tv_usec = 1;
126 }
127
128 static int loglevel;
129 INIT_STDERR_LOGGING(loglevel)
130
131 static struct writer_node_group *check_args(void)
132 {
133 int i, ret = -E_WRITE_SYNTAX;
134 struct writer_node_group *g = NULL;
135 struct initial_delay_task *idt = &the_initial_delay_task;
136
137 loglevel = get_loglevel_by_name(conf.loglevel_arg);
138 if (conf.start_time_given) {
139 long unsigned sec, usec;
140 if (sscanf(conf.start_time_arg, "%lu:%lu",
141 &sec, &usec) != 2)
142 goto out;
143 idt->start_time.tv_sec = sec;
144 idt->start_time.tv_usec = usec;
145 }
146 if (!conf.writer_given) {
147 g = setup_default_wng();
148 ret = 1;
149 goto out;
150 }
151 g = wng_new(conf.writer_given);
152 ret = -E_WRITE_SYNTAX;
153 for (i = 0; i < conf.writer_given; i++) {
154 int writer_num;
155 g->writer_nodes[i].conf = check_writer_arg(
156 conf.writer_arg[i], &writer_num);
157 if (!g->writer_nodes[i].conf)
158 goto out;
159 g->writer_nodes[i].writer_num = writer_num;
160 }
161 ret = 1;
162 out:
163 if (ret > 0)
164 return g;
165 free(g);
166 return NULL;
167 }
168
169 __noreturn static void print_help_and_die(void)
170 {
171 int d = conf.detailed_help_given;
172 const char **p = d? write_args_info_detailed_help
173 : write_args_info_help;
174
175 printf_or_die("%s\n\n", WRITE_CMDLINE_PARSER_PACKAGE "-"
176 WRITE_CMDLINE_PARSER_VERSION);
177 printf_or_die("%s\n\n", write_args_info_usage);
178 for (; *p; p++)
179 printf_or_die("%s\n", *p);
180 print_writer_helps(d);
181 exit(0);
182 }
183
184 /*
185 TODO: check wav, initial delay, multiple writers, non-default writers
186 */
187 static int main_btr(struct sched *s)
188 {
189 struct writer_node *wn = para_malloc(sizeof(*wn));
190 struct writer *w = writers + DEFAULT_WRITER;
191 int ret;
192
193 wn->writer_num = DEFAULT_WRITER;
194 wn->conf = writers[DEFAULT_WRITER].parse_config("-B");
195 sit.btrn = btr_new_node("stdin", NULL /* stdin has no parent */, NULL);
196 stdin_set_defaults(&sit);
197 register_task(&sit.task);
198
199 wn->btrn = btr_new_node("writer", sit.btrn, NULL);
200
201 sprintf(wn->task.status, "some writer");
202 w->open(wn);
203 wn->task.post_select = w->post_select_btr;
204 wn->task.pre_select = w->pre_select_btr;
205 register_task(&wn->task);
206
207 s->default_timeout.tv_sec = 10;
208 s->default_timeout.tv_usec = 50000;
209 ret = schedule(s);
210 w->close(wn);
211 return ret;
212 }
213
214 /**
215 * Para_write's main function.
216 *
217 * \param argc The usual argument counter.
218 * \param argv The usual argument vector.
219 *
220 * It registers the stdin task, the check_wav_task, the task for initial delay
221 * and all tasks for actually writing out the stream.
222 *
223 * \return \p EXIT_SUCCESS or EXIT_FAILURE
224 */
225 int main(int argc, char *argv[])
226 {
227 int ret = -E_WRITE_SYNTAX;
228 static struct sched s;
229 struct check_wav_task *cwt = &the_check_wav_task;
230 struct initial_delay_task *idt = &the_initial_delay_task;
231
232 writer_init();
233 write_cmdline_parser(argc, argv, &conf);
234 HANDLE_VERSION_FLAG("write", conf);
235 if (conf.help_given || conf.detailed_help_given)
236 print_help_and_die();
237
238 if (conf.buffer_tree_given) {
239 ret = main_btr(&s);
240 goto out;
241 }
242 wng = check_args();
243 if (!wng)
244 goto out;
245 stdin_set_defaults(&sit);
246 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
247 if (conf.bufsize_arg < 0)
248 goto out;
249 if (conf.bufsize_arg >= INT_MAX / 1024)
250 goto out;
251 sit.bufsize = conf.bufsize_arg * 1024;
252 sit.buf = para_malloc(sit.bufsize);
253
254 wng->bufp = &sit.buf;
255 wng->loaded = &sit.loaded;
256 wng->input_error = &sit.task.error;
257
258 register_task(&sit.task);
259
260 cwt->buf = sit.buf;
261 cwt->loaded = &sit.loaded;
262 cwt->input_error = &sit.task.error;
263 sprintf(cwt->task.status, "check wav");
264 cwt->task.pre_select = check_wav_pre_select;
265
266 idt->task.pre_select = initial_delay_pre_select;
267 sprintf(idt->task.status, "initial_delay");
268 register_task(&idt->task);
269
270 s.default_timeout.tv_sec = 10;
271 s.default_timeout.tv_usec = 0;
272 ret = schedule(&s);
273 wng_close(wng);
274 out:
275 if (ret < 0) {
276 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
277 exit(EXIT_FAILURE);
278 }
279 exit(EXIT_SUCCESS);
280 }