Deduplicate --channels and --sample-rate.
[paraslash.git] / write.c
1 /*
2  * Copyright (C) 2005-2010 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
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 struct check_wav_task {
36         int state;
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 sample_rate;
41         /** The task structure used by the scheduler. */
42         struct task task;
43         struct btr_node *btrn;
44         size_t min_iqs;
45 };
46
47 static struct write_args_info conf;
48
49 static struct stdin_task sit;
50
51 /** Length of a standard wav header. */
52 #define WAV_HEADER_LEN 44
53
54 /**
55  * Test if audio buffer contains a valid wave header.
56  *
57  * \return If not, return -E_NO_WAV_HEADER, otherwise, return zero. If
58  * there is less than WAV_HEADER_LEN bytes available, return one.
59  */
60 static void check_wav_pre_select(struct sched *s, struct task *t)
61 {
62         struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
63         int ret;
64
65         ret = btr_node_status(cwt->btrn, cwt->min_iqs, BTR_NT_INTERNAL);
66         if (ret != 0)
67                 sched_min_delay(s);
68 }
69
70 #define HANDLE_EXEC(_cmd) \
71         if (!strcmp(cmd, #_cmd)) { \
72                 if (!conf._cmd ## _given && cwt->state == CWS_NEED_HEADER) \
73                         return -E_BTR_NAVAIL; \
74                 *result = make_message("%d", cwt->state == CWS_NO_HEADER? \
75                         conf._cmd ## _arg : cwt->_cmd); \
76                 return 1; \
77         } \
78
79
80 static int check_wav_exec(struct btr_node *btrn, const char *cmd, char **result)
81 {
82         struct check_wav_task *cwt = btr_context(btrn);
83
84         HANDLE_EXEC(sample_rate);
85         HANDLE_EXEC(channels);
86         return -ERRNO_TO_PARA_ERROR(ENOTSUP);
87 }
88
89 static void check_wav_post_select(__a_unused struct sched *s, struct task *t)
90 {
91         struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
92         struct btr_node *btrn = cwt->btrn;
93         unsigned char *a;
94         size_t sz;
95         int ret;
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         cwt->channels = 2;
109         cwt->sample_rate = 44100;
110         if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F') {
111                 PARA_NOTICE_LOG("wav header not found\n");
112                 cwt->state = CWS_NO_HEADER;
113                 sprintf(t->status, "check wav: no header");
114                 goto out;
115         }
116         PARA_INFO_LOG("found wav header\n");
117         cwt->state = CWS_HAVE_HEADER;
118         sprintf(t->status, "check wav: have header");
119         cwt->channels = (unsigned) a[22];
120         cwt->sample_rate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
121         PARA_INFO_LOG("channels: %d, sample rate: %d\n", cwt->channels, cwt->sample_rate);
122         btr_consume(btrn, WAV_HEADER_LEN);
123 pushdown:
124         btr_pushdown(btrn);
125 out:
126         t->error = ret;
127         if (ret < 0)
128                 btr_remove_node(btrn);
129 }
130
131 static int loglevel;
132 INIT_STDERR_LOGGING(loglevel)
133
134 __noreturn static void print_help_and_die(void)
135 {
136         int d = conf.detailed_help_given;
137         const char **p = d? write_args_info_detailed_help
138                 : write_args_info_help;
139
140         printf_or_die("%s\n\n", WRITE_CMDLINE_PARSER_PACKAGE "-"
141                 WRITE_CMDLINE_PARSER_VERSION);
142         printf_or_die("%s\n\n", write_args_info_usage);
143         for (; *p; p++)
144                 printf_or_die("%s\n", *p);
145         print_writer_helps(d);
146         exit(0);
147 }
148
149 static int main_btr(struct sched *s)
150 {
151         int i, ret;
152         struct check_wav_task _cwt, *cwt = &_cwt;
153         struct writer_node *wns;
154
155         loglevel = get_loglevel_by_name(conf.loglevel_arg);
156         sit.btrn = btr_new_node(&(struct btr_node_description)
157                 EMBRACE(.name = "stdin"));
158         stdin_set_defaults(&sit);
159         register_task(&sit.task);
160
161         cwt->state = CWS_NEED_HEADER;
162         cwt->min_iqs = WAV_HEADER_LEN;
163         cwt->btrn = btr_new_node(&(struct btr_node_description)
164                 EMBRACE(.name = "check_wav", .parent = sit.btrn,
165                 .handler = check_wav_exec, .context = cwt));
166         sprintf(cwt->task.status, "check_wav");
167         cwt->task.pre_select = check_wav_pre_select;
168         cwt->task.post_select = check_wav_post_select;
169         cwt->task.error = 0;
170         register_task(&cwt->task);
171
172         ret = -E_WRITE_SYNTAX;
173         if (!conf.writer_given) {
174                 i = 0;
175                 wns = para_calloc(sizeof(*wns));
176                 ret = setup_writer_node(NULL, cwt->btrn, wns);
177                 if (ret < 0)
178                         goto out;
179                 i = 1;
180         } else {
181                 wns = para_calloc(conf.writer_given * sizeof(*wns));
182                 for (i = 0; i < conf.writer_given; i++) {
183                         ret = setup_writer_node(conf.writer_arg[i],
184                                 cwt->btrn, wns + i);
185                         if (ret < 0)
186                                 goto out;
187                 }
188         }
189
190         s->default_timeout.tv_sec = 10;
191         s->default_timeout.tv_usec = 50000;
192         ret = schedule(s);
193 out:
194         for (i--; i >= 0; i--) {
195                 struct writer_node *wn = wns + i;
196                 struct writer *w = writers + wn->writer_num;
197
198                 w->close(wn);
199                 btr_free_node(wn->btrn);
200                 free(wn->conf);
201         }
202         free(wns);
203         btr_free_node(cwt->btrn);
204         return ret;
205 }
206
207 /**
208  * Para_write's main function.
209  *
210  * \param argc The usual argument counter.
211  * \param argv The usual argument vector.
212  *
213  * It sets up and starts the tasks and the buffer tree nodes determined by
214  * command line options.
215  *
216  * \return \p EXIT_SUCCESS or EXIT_FAILURE
217  */
218 int main(int argc, char *argv[])
219 {
220         int ret = -E_WRITE_SYNTAX;
221         static struct sched s;
222
223         writer_init();
224         write_cmdline_parser(argc, argv, &conf);
225         HANDLE_VERSION_FLAG("write", conf);
226         if (conf.help_given || conf.detailed_help_given)
227                 print_help_and_die();
228
229         ret = main_btr(&s);
230         if (ret < 0) {
231                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
232                 exit(EXIT_FAILURE);
233         }
234         exit(EXIT_SUCCESS);
235 }