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