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