server: Fix brown paper bag bug in generic_com_on().
[paraslash.git] / check_wav.c
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file check_wav.c Detect and delete a wav header. */
4
5 #include <regex.h>
6
7 #include "para.h"
8 #include "string.h"
9 #include "list.h"
10 #include "sched.h"
11 #include "buffer_tree.h"
12 #include "error.h"
13 #include "check_wav.h"
14
15 /** Length of a standard wav header. */
16 #define WAV_HEADER_LEN 44
17
18 /** The possible states of a check_wav instance. */
19 enum check_wav_state {
20         /** Initial state, less than \p WAV_HEADER_LEN bytes available. */
21         CWS_NEED_HEADER,
22         /** Wav hader was detected. */
23         CWS_HAVE_HEADER,
24         /** First part of the stream did not look like a wav header. */
25         CWS_NO_HEADER,
26 };
27
28 struct check_wav_context {
29         enum check_wav_state state;
30         struct btr_node *btrn;
31         size_t min_iqs;
32         /* Command line args. */
33         struct wav_params params;
34         /* Extracted from the wav header.*/
35         unsigned channels;
36         unsigned sample_format;
37         unsigned sample_rate;
38 };
39
40 /**
41  * Set select timeout according to the given context.
42  *
43  * \param s Contains the timeval that should be set.
44  * \param cwc Contains a pointer to the buffer tree node.
45  *
46  * This requests a minimal timeout from the scheduler if btrn of \a cwc is not
47  * idle.
48  */
49 void check_wav_pre_select(struct sched *s, struct check_wav_context *cwc)
50 {
51         int ret = btr_node_status(cwc->btrn, cwc->min_iqs, BTR_NT_INTERNAL);
52         if (ret != 0)
53                 sched_min_delay(s);
54 }
55
56 static int check_wav_exec(struct btr_node *btrn, const char *cmd, char **result)
57 {
58         struct check_wav_context *cwc = btr_context(btrn);
59         int val, header_val, given, arg;
60
61         header_val = cwc->channels;
62         arg = cwc->params.channels_arg;
63         given = cwc->params.channels_given;
64         if (!strcmp(cmd, "channels"))
65                 goto out;
66
67         header_val = cwc->sample_rate;
68         arg = cwc->params.sample_rate_arg;
69         given = cwc->params.sample_rate_given;
70         if (!strcmp(cmd, "sample_rate"))
71                 goto out;
72
73         header_val = cwc->sample_format;
74         arg = cwc->params.sample_format_arg;
75         given = cwc->params.sample_format_given;
76         if (!strcmp(cmd, "sample_format"))
77                 goto out;
78
79         return -ERRNO_TO_PARA_ERROR(ENOTSUP);
80 out:
81         if (given)
82                 val = arg;
83         else {
84                 switch (cwc->state) {
85                 case CWS_HAVE_HEADER:
86                         val = header_val;
87                         break;
88                 case CWS_NO_HEADER:
89                         /*
90                          * No wav header available and no value specified at
91                          * the command line. Maybe one of our parent nodes
92                          * knows.
93                          */
94                         if (btr_exec_up(btr_parent(cwc->btrn), cmd, result) >= 0)
95                                 return 1;
96                         /* Use default value */
97                         val = arg;
98                         break;
99                 default:
100                         return -E_BTR_NAVAIL;
101                 }
102         }
103         *result = make_message("%d", val);
104         return 1;
105 }
106
107 /**
108  * Filter out the wav header, pushdown everything else.
109  *
110  * \param cwc The context of this instance.
111  *
112  * This function looks at the first \p WAV_HEADER_SIZE bytes of the input queue
113  * of the btrn of \a cwc. If they look like a wav header, the function extracts
114  * the information of interest and swallows this part of the stream. Otherwise
115  * it is pushed down to all children. In either case the rest of the input is
116  * pushed down as well.
117  *
118  * Once the first part has been processed this way, the state of the instance
119  * changes from \p CWS_NEED_HEADER to \p CWS_HAVE_HEADER or \p CWS_NO_HEADER.
120  *
121  * \return Standard.
122  */
123 int check_wav_post_select(struct check_wav_context *cwc)
124 {
125         struct btr_node *btrn = cwc->btrn;
126         unsigned char *a;
127         size_t sz;
128         int ret;
129         uint16_t bps; /* bits per sample */
130         const char *sample_formats[] = {SAMPLE_FORMATS};
131
132         if (!btrn)
133                 return 0;
134         ret = btr_node_status(btrn, cwc->min_iqs, BTR_NT_INTERNAL);
135         if (ret <= 0)
136                 goto out;
137         if (cwc->state != CWS_NEED_HEADER)
138                 goto pushdown;
139         btr_merge(btrn, cwc->min_iqs);
140         sz = btr_next_buffer(btrn, (char **)&a);
141         if (sz < cwc->min_iqs) /* file size less than WAV_HEADER_SIZE */
142                 goto pushdown;
143         cwc->min_iqs = 0;
144         /*
145          * The default byte ordering assumed for WAVE data files is
146          * little-endian. Files written using the big-endian byte ordering
147          * scheme have the identifier RIFX instead of RIFF.
148          */
149         if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' ||
150                         (a[3] != 'F' && a[3] != 'X')) {
151                 PARA_NOTICE_LOG("wav header not found\n");
152                 cwc->state = CWS_NO_HEADER;
153                 goto out;
154         }
155         PARA_INFO_LOG("found wav header\n");
156         cwc->state = CWS_HAVE_HEADER;
157         /* Only set those values which have not already been set. */
158         cwc->channels = (unsigned)a[22];
159         cwc->sample_rate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
160         bps = a[34] + ((unsigned)a[35] << 8);
161         if (bps != 8 && bps != 16) {
162                 PARA_WARNING_LOG("%u bps not supported, assuming 16\n",
163                         bps);
164                 bps = 16;
165         }
166         /*
167          * 8-bit samples are stored as unsigned bytes, ranging from 0
168          * to 255.  16-bit samples are stored as 2's-complement signed
169          * integers, ranging from -32768 to 32767.
170          */
171         if (bps == 8)
172                 cwc->sample_format = SF_U8;
173         else
174                 cwc->sample_format = (a[3] == 'F')?
175                         SF_S16_LE : SF_S16_BE;
176         PARA_NOTICE_LOG("%uHz, %s, %s\n", cwc->sample_rate,
177                 cwc->channels == 1? "mono" : "stereo",
178                 sample_formats[cwc->sample_format]);
179         btr_consume(btrn, WAV_HEADER_LEN);
180 pushdown:
181         btr_pushdown(btrn);
182 out:
183         if (ret < 0)
184                 btr_remove_node(&cwc->btrn);
185         return ret;
186 }
187
188 /**
189  * Allocate and set up a new check_wav instance.
190  *
191  * \param parent This buffer tree node will be the parent of the new node.
192  * \param child The child of the new node.
193  * \param params Default values and options.
194  * \param cw_btrn A pointer to the check wav node is returned here.
195  *
196  * This function also sets up the ->execute handler of the btrn so that all
197  * children of this node can figure out channel count, sample rate, etc.
198  *
199  * \return The (opaque) handle of the newly created check_wav instance. It is
200  * supposed to be passed to \ref check_wav_pre_select() and \ref
201  * check_wav_post_select().
202  *
203  * \sa \ref btr_new_node.
204  */
205 struct check_wav_context *check_wav_init(struct btr_node *parent,
206                 struct btr_node *child, struct wav_params *params,
207                 struct btr_node **cw_btrn)
208 {
209         struct check_wav_context *cwc = para_calloc(sizeof(*cwc));
210
211         cwc->state = CWS_NEED_HEADER;
212         cwc->min_iqs = WAV_HEADER_LEN;
213         cwc->params = *params;
214         cwc->btrn = btr_new_node(&(struct btr_node_description)
215                 EMBRACE(.name = "check_wav", .parent = parent, .child = child,
216                 .handler = check_wav_exec, .context = cwc));
217         if (cw_btrn)
218                 *cw_btrn = cwc->btrn;
219         return cwc;
220 }
221
222 /**
223  * Dellocate all ressources of a check_wav instance.
224  *
225  * \param cwc Determines the instance to shut down.
226  *
227  * This function may only be called after check_wav_post_select() has returned
228  * negative.
229  */
230 void check_wav_shutdown(struct check_wav_context *cwc)
231 {
232         free(cwc);
233 }