Move setup_writer_node() from write_common.c to write.c.
[paraslash.git] / write.c
1 /*
2 * Copyright (C) 2005-2011 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 <stdbool.h>
12
13 #include "para.h"
14 #include "string.h"
15 #include "write.cmdline.h"
16 #include "list.h"
17 #include "sched.h"
18 #include "ggo.h"
19 #include "stdin.h"
20 #include "buffer_tree.h"
21 #include "write.h"
22 #include "write_common.h"
23 #include "fd.h"
24 #include "error.h"
25 #include "version.h"
26
27 INIT_WRITE_ERRLISTS;
28
29 enum check_wav_state {
30 CWS_NEED_HEADER,
31 CWS_HAVE_HEADER,
32 CWS_NO_HEADER,
33 };
34
35 /* Information extracted from the wav header. */
36 struct check_wav_task {
37 int state;
38 /** Number of channels. */
39 unsigned channels;
40 unsigned sample_format;
41 /** Sample rate specified in wav header given by \a buf. */
42 unsigned sample_rate;
43 /** The task structure used by the scheduler. */
44 struct task task;
45 struct btr_node *btrn;
46 size_t min_iqs;
47 };
48
49 static struct write_args_info conf;
50
51 static struct stdin_task sit;
52
53 /** Length of a standard wav header. */
54 #define WAV_HEADER_LEN 44
55
56 static void check_wav_pre_select(struct sched *s, struct task *t)
57 {
58 struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
59 int ret;
60
61 ret = btr_node_status(cwt->btrn, cwt->min_iqs, BTR_NT_INTERNAL);
62 if (ret != 0)
63 sched_min_delay(s);
64 }
65
66 #define HANDLE_EXEC(_cmd) \
67 if (!strcmp(cmd, #_cmd)) { \
68 if (!conf._cmd ## _given && cwt->state == CWS_NEED_HEADER) \
69 return -E_BTR_NAVAIL; \
70 *result = make_message("%d", cwt->state == CWS_NO_HEADER || conf._cmd ## _given? \
71 conf._cmd ## _arg : cwt->_cmd); \
72 return 1; \
73 } \
74
75
76 static int check_wav_exec(struct btr_node *btrn, const char *cmd, char **result)
77 {
78 struct check_wav_task *cwt = btr_context(btrn);
79
80 HANDLE_EXEC(sample_rate);
81 HANDLE_EXEC(channels);
82 HANDLE_EXEC(sample_format);
83 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
84 }
85
86 static void check_wav_post_select(__a_unused struct sched *s, struct task *t)
87 {
88 struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
89 struct btr_node *btrn = cwt->btrn;
90 unsigned char *a;
91 size_t sz;
92 int ret;
93 uint16_t bps; /* bits per sample */
94 const char *sample_formats[] = {SAMPLE_FORMATS};
95
96 t->error = 0;
97 ret = btr_node_status(btrn, cwt->min_iqs, BTR_NT_INTERNAL);
98 if (ret <= 0)
99 goto out;
100 if (cwt->state != CWS_NEED_HEADER)
101 goto pushdown;
102 btr_merge(btrn, cwt->min_iqs);
103 sz = btr_next_buffer(btrn, (char **)&a);
104 if (sz < cwt->min_iqs) /* file size less than WAV_HEADER_SIZE */
105 goto pushdown;
106 cwt->min_iqs = 0;
107 /*
108 * The default byte ordering assumed for WAVE data files is
109 * little-endian. Files written using the big-endian byte ordering
110 * scheme have the identifier RIFX instead of RIFF.
111 */
112 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' ||
113 (a[3] != 'F' && a[3] != 'X')) {
114 PARA_NOTICE_LOG("wav header not found\n");
115 cwt->state = CWS_NO_HEADER;
116 sprintf(t->status, "check wav: no header");
117 goto out;
118 }
119 PARA_INFO_LOG("found wav header\n");
120 cwt->state = CWS_HAVE_HEADER;
121 sprintf(t->status, "check wav: have header");
122 cwt->channels = (unsigned) a[22];
123 cwt->sample_rate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
124 bps = a[34] + ((unsigned)a[35] << 8);
125 if (bps != 8 && bps != 16) {
126 PARA_WARNING_LOG("%u bps not supported, assuming 16\n", bps);
127 bps = 16;
128 }
129 /*
130 * 8-bit samples are stored as unsigned bytes, ranging from 0 to 255.
131 * 16-bit samples are stored as 2's-complement signed integers, ranging
132 * from -32768 to 32767.
133 */
134 if (bps == 8)
135 cwt->sample_format = SF_U8;
136 else
137 cwt->sample_format = (a[3] == 'F')? SF_S16_LE : SF_S16_BE;
138 PARA_NOTICE_LOG("%dHz, %s, %s\n", cwt->sample_rate,
139 cwt->channels == 1? "mono" : "stereo",
140 sample_formats[cwt->sample_format]);
141 btr_consume(btrn, WAV_HEADER_LEN);
142 pushdown:
143 btr_pushdown(btrn);
144 out:
145 t->error = ret;
146 if (ret < 0)
147 btr_remove_node(btrn);
148 }
149
150 static int loglevel;
151 INIT_STDERR_LOGGING(loglevel)
152
153 __noreturn static void print_help_and_die(void)
154 {
155 int d = conf.detailed_help_given;
156 const char **p = d? write_args_info_detailed_help
157 : write_args_info_help;
158
159 printf_or_die("%s\n\n", WRITE_CMDLINE_PARSER_PACKAGE "-"
160 WRITE_CMDLINE_PARSER_VERSION);
161 printf_or_die("%s\n\n", write_args_info_usage);
162 for (; *p; p++)
163 printf_or_die("%s\n", *p);
164 print_writer_helps(d);
165 exit(0);
166 }
167
168 /*
169 * Parse config and register a task for a writer node.
170 *
171 * \param arg Command line arguments.
172 * \param parent The new node will be a child of \a parent.
173 * \param wn The writer node.
174 *
175 * If arg is \p NULL, the OS-dependent default writer is used with no
176 * arguments. The default writers are alsa for Linux, osx for OS X, oss for
177 * *BSD, and the file writer if the default writer is not supported.
178 *
179 * Once the writer configuration has been retrieved from the ->parse_config
180 * callback a writer node is created, its buffer tree node is added to the
181 * buffer tree as a child of the given parent.
182 *
183 * Finally, the new writer node's task structure is initialized and registered
184 * to the paraslash scheduler.
185 *
186 * \return Standard.
187 */
188 static int setup_writer_node(const char *arg, struct btr_node *parent,
189 struct writer_node *wn)
190 {
191 if (arg)
192 wn->conf = check_writer_arg(arg, &wn->writer_num);
193 else {
194 wn->writer_num = DEFAULT_WRITER;
195 wn->conf = writers[DEFAULT_WRITER].parse_config_or_die("");
196 }
197 if (!wn->conf)
198 return -E_WRITE_COMMON_SYNTAX;
199 register_writer_node(wn, parent);
200 return 1;
201 }
202
203 static int main_btr(struct sched *s)
204 {
205 int i, ret;
206 struct check_wav_task _cwt, *cwt = &_cwt;
207 struct writer_node *wns;
208
209 loglevel = get_loglevel_by_name(conf.loglevel_arg);
210 sit.btrn = btr_new_node(&(struct btr_node_description)
211 EMBRACE(.name = "stdin"));
212 stdin_set_defaults(&sit);
213 register_task(&sit.task);
214
215 cwt->state = CWS_NEED_HEADER;
216 cwt->min_iqs = WAV_HEADER_LEN;
217 cwt->btrn = btr_new_node(&(struct btr_node_description)
218 EMBRACE(.name = "check_wav", .parent = sit.btrn,
219 .handler = check_wav_exec, .context = cwt));
220 sprintf(cwt->task.status, "check_wav");
221 cwt->task.pre_select = check_wav_pre_select;
222 cwt->task.post_select = check_wav_post_select;
223 cwt->task.error = 0;
224 register_task(&cwt->task);
225
226 ret = -E_WRITE_SYNTAX;
227 if (!conf.writer_given) {
228 i = 0;
229 wns = para_calloc(sizeof(*wns));
230 ret = setup_writer_node(NULL, cwt->btrn, wns);
231 if (ret < 0)
232 goto out;
233 i = 1;
234 } else {
235 wns = para_calloc(conf.writer_given * sizeof(*wns));
236 for (i = 0; i < conf.writer_given; i++) {
237 ret = setup_writer_node(conf.writer_arg[i],
238 cwt->btrn, wns + i);
239 if (ret < 0)
240 goto out;
241 }
242 }
243
244 s->default_timeout.tv_sec = 10;
245 s->default_timeout.tv_usec = 50000;
246 ret = schedule(s);
247 if (ret >= 0) {
248 int j;
249 for (j = 0; j < i; j++) {
250 struct task *t = &wns[j].task;
251 assert(t->error < 0);
252 if (t->error != -E_WRITE_COMMON_EOF
253 && t->error != -E_BTR_EOF) {
254 PARA_ERROR_LOG("%s: %s\n", t->status,
255 para_strerror(-t->error));
256 if (ret >= 0)
257 ret = t->error;
258 }
259 }
260 }
261 out:
262 for (i--; i >= 0; i--) {
263 struct writer_node *wn = wns + i;
264 struct writer *w = writers + wn->writer_num;
265
266 w->close(wn);
267 btr_free_node(wn->btrn);
268 w->free_config(wn->conf);
269 free(wn->conf);
270 }
271 free(wns);
272 btr_free_node(cwt->btrn);
273 return ret;
274 }
275
276 /**
277 * Para_write's main function.
278 *
279 * \param argc The usual argument counter.
280 * \param argv The usual argument vector.
281 *
282 * It sets up and starts the tasks and the buffer tree nodes determined by
283 * command line options.
284 *
285 * \return \p EXIT_SUCCESS or EXIT_FAILURE
286 */
287 int main(int argc, char *argv[])
288 {
289 int ret = -E_WRITE_SYNTAX;
290 static struct sched s;
291
292 writer_init();
293 write_cmdline_parser(argc, argv, &conf);
294 HANDLE_VERSION_FLAG("write", conf);
295 if (conf.help_given || conf.detailed_help_given)
296 print_help_and_die();
297
298 ret = main_btr(&s);
299 if (ret < 0) {
300 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
301 exit(EXIT_FAILURE);
302 }
303 exit(EXIT_SUCCESS);
304 }