Change year in COPYRIGHT to 2010.
[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 samplerate;
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 static int check_wav_exec(struct btr_node *btrn, const char *cmd, char **result)
71 {
72 struct check_wav_task *cwt = btr_context(btrn);
73
74
75 if (!strcmp(cmd, "samplerate")) {
76 if (cwt->state != CWS_HAVE_HEADER)
77 return -E_BTR_NAVAIL;
78 *result = make_message("%d", cwt->samplerate);
79 return 1;
80 }
81 if (!strcmp(cmd, "channels")) {
82 if (cwt->state != CWS_HAVE_HEADER)
83 return -E_BTR_NAVAIL;
84 *result = make_message("%d", cwt->channels);
85 return 1;
86 }
87 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
88 }
89
90 static void check_wav_post_select(__a_unused struct sched *s, struct task *t)
91 {
92 struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
93 struct btr_node *btrn = cwt->btrn;
94 unsigned char *a;
95 size_t sz;
96 int ret;
97
98 t->error = 0;
99 ret = btr_node_status(btrn, cwt->min_iqs, BTR_NT_INTERNAL);
100 if (ret <= 0)
101 goto out;
102 if (cwt->state != CWS_NEED_HEADER)
103 goto pushdown;
104 btr_merge(btrn, cwt->min_iqs);
105 sz = btr_next_buffer(btrn, (char **)&a);
106 if (sz < cwt->min_iqs) /* file size less than WAV_HEADER_SIZE */
107 goto pushdown;
108 cwt->min_iqs = 0;
109 cwt->channels = 2;
110 cwt->samplerate = 44100;
111 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F') {
112 PARA_NOTICE_LOG("wav header not found\n");
113 cwt->state = CWS_NO_HEADER;
114 sprintf(t->status, "check wav: no header");
115 goto out;
116 }
117 PARA_INFO_LOG("found wav header\n");
118 cwt->state = CWS_HAVE_HEADER;
119 sprintf(t->status, "check wav: have header");
120 cwt->channels = (unsigned) a[22];
121 cwt->samplerate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
122 PARA_INFO_LOG("channels: %d, sample rate: %d\n", cwt->channels, cwt->samplerate);
123 btr_consume(btrn, WAV_HEADER_LEN);
124 pushdown:
125 btr_pushdown(btrn);
126 out:
127 t->error = ret;
128 if (ret < 0)
129 btr_remove_node(btrn);
130 }
131
132 static int loglevel;
133 INIT_STDERR_LOGGING(loglevel)
134
135 __noreturn static void print_help_and_die(void)
136 {
137 int d = conf.detailed_help_given;
138 const char **p = d? write_args_info_detailed_help
139 : write_args_info_help;
140
141 printf_or_die("%s\n\n", WRITE_CMDLINE_PARSER_PACKAGE "-"
142 WRITE_CMDLINE_PARSER_VERSION);
143 printf_or_die("%s\n\n", write_args_info_usage);
144 for (; *p; p++)
145 printf_or_die("%s\n", *p);
146 print_writer_helps(d);
147 exit(0);
148 }
149
150 static int main_btr(struct sched *s)
151 {
152 int i, ret;
153 struct check_wav_task _cwt, *cwt = &_cwt;
154 struct writer_node *wns;
155
156 loglevel = get_loglevel_by_name(conf.loglevel_arg);
157 sit.btrn = btr_new_node(&(struct btr_node_description)
158 EMBRACE(.name = "stdin"));
159 stdin_set_defaults(&sit);
160 register_task(&sit.task);
161
162 cwt->state = CWS_NEED_HEADER;
163 cwt->min_iqs = WAV_HEADER_LEN;
164 cwt->btrn = btr_new_node(&(struct btr_node_description)
165 EMBRACE(.name = "check_wav", .parent = sit.btrn,
166 .handler = check_wav_exec, .context = cwt));
167 sprintf(cwt->task.status, "check_wav");
168 cwt->task.pre_select = check_wav_pre_select;
169 cwt->task.post_select = check_wav_post_select;
170 cwt->task.error = 0;
171 register_task(&cwt->task);
172
173 ret = -E_WRITE_SYNTAX;
174 if (!conf.writer_given) {
175 i = 0;
176 wns = para_calloc(sizeof(*wns));
177 ret = setup_writer_node(NULL, cwt->btrn, wns);
178 if (ret < 0)
179 goto out;
180 i = 1;
181 } else {
182 wns = para_calloc(conf.writer_given * sizeof(*wns));
183 for (i = 0; i < conf.writer_given; i++) {
184 ret = setup_writer_node(conf.writer_arg[i],
185 cwt->btrn, wns + i);
186 if (ret < 0)
187 goto out;
188 }
189 }
190
191 s->default_timeout.tv_sec = 10;
192 s->default_timeout.tv_usec = 50000;
193 ret = schedule(s);
194 out:
195 for (i--; i >= 0; i--) {
196 struct writer_node *wn = wns + i;
197 struct writer *w = writers + wn->writer_num;
198
199 w->close(wn);
200 btr_free_node(wn->btrn);
201 free(wn->conf);
202 }
203 free(wns);
204 btr_free_node(cwt->btrn);
205 return ret;
206 }
207
208 /**
209 * Para_write's main function.
210 *
211 * \param argc The usual argument counter.
212 * \param argv The usual argument vector.
213 *
214 * It sets up and starts the tasks and the buffer tree nodes determined by
215 * command line options.
216 *
217 * \return \p EXIT_SUCCESS or EXIT_FAILURE
218 */
219 int main(int argc, char *argv[])
220 {
221 int ret = -E_WRITE_SYNTAX;
222 static struct sched s;
223
224 writer_init();
225 write_cmdline_parser(argc, argv, &conf);
226 HANDLE_VERSION_FLAG("write", conf);
227 if (conf.help_given || conf.detailed_help_given)
228 print_help_and_die();
229
230 ret = main_btr(&s);
231 if (ret < 0) {
232 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
233 exit(EXIT_FAILURE);
234 }
235 exit(EXIT_SUCCESS);
236 }