first version of the universal paraslash scheduler
[paraslash.git] / write.c
1 /*
2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 #include "para.h"
20 #include "string.h"
21 #include "write.cmdline.h"
22 #include "list.h"
23 #include "sched.h"
24 #include "stdin.h"
25 #include "write.h"
26 #include "write_common.h"
27 #include "fd.h"
28 #include "error.h"
29
30 INIT_WRITE_ERRLISTS;
31
32 struct check_wav_task {
33 char *buf;
34 size_t *loaded;
35 unsigned channels;
36 unsigned sample_rate;
37 struct task task;
38 };
39
40 struct initial_delay_task {
41 struct timeval start_time;
42 struct task task;
43 };
44
45 struct gengetopt_args_info conf;
46 struct stdin_task sit;
47 struct check_wav_task cwt;
48 struct initial_delay_task idt;
49 static struct writer_node_group *wng;
50
51 #define WAV_HEADER_LEN 44
52
53 /**
54 * test if audio buffer contains a valid wave header
55 *
56 * \return If not, return 0, otherwise, store number of channels and sample rate
57 * in struct conf and return WAV_HEADER_LEN.
58 */
59 static void check_wav_pre_select(struct sched *s, struct task *t)
60 {
61 struct check_wav_task *cwt = t->private_data;
62 unsigned char *a;
63
64 if (*cwt->loaded < WAV_HEADER_LEN) {
65 t->ret = 1;
66 return;
67 }
68 a = (unsigned char*)cwt->buf;
69 t->ret = -E_NO_WAV_HEADER;
70 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F')
71 return;
72 cwt->channels = (unsigned) a[22];
73 cwt->sample_rate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
74 *cwt->loaded -= WAV_HEADER_LEN;
75 memmove(cwt->buf, cwt->buf + WAV_HEADER_LEN, *cwt->loaded);
76 t->ret = 0;
77 PARA_INFO_LOG("channels: %d, sample_rate: %d\n", cwt->channels, cwt->sample_rate);
78 }
79
80 static void initial_delay_pre_select(struct sched *s, struct task *t)
81 {
82 struct initial_delay_task *idt = t->private_data;
83 struct timeval diff;
84
85 PARA_ERROR_LOG("task %p, ret: %d\n", t, t->ret);
86 t->ret = -E_NO_DELAY;
87 if (!idt->start_time.tv_sec && !idt->start_time.tv_usec)
88 return;
89 t->ret = 0; /* timeout */
90 if (tv_diff(&s->now, &idt->start_time, &diff) > 0)
91 return;
92 t->ret = 1;
93 if (tv_diff(&s->timeout , &diff, NULL) > 0)
94 s->timeout = diff;
95 }
96
97 void para_log(int ll, const char* fmt,...)
98 {
99 va_list argp;
100
101 if (ll < conf.loglevel_arg)
102 return;
103 va_start(argp, fmt);
104 vfprintf(stderr, fmt, argp);
105 va_end(argp);
106 }
107
108 static struct writer_node_group *check_args(void)
109 {
110 int i, ret = -E_WRITE_SYNTAX;
111 struct writer_node_group *wng = NULL;
112
113 if (conf.list_writers_given) {
114 char *msg = NULL;
115 FOR_EACH_WRITER(i) {
116 char *tmp = make_message("%s%s%s",
117 i? msg : "",
118 i? " " : "",
119 writer_names[i]);
120 free(msg);
121 msg = tmp;
122 }
123 fprintf(stderr, "%s\n", msg);
124 free(msg);
125 exit(EXIT_SUCCESS);
126 }
127 if (conf.prebuffer_arg < 0 || conf.prebuffer_arg > 100)
128 goto out;
129 if (conf.start_time_given) {
130 long unsigned sec, usec;
131 if (sscanf(conf.start_time_arg, "%lu:%lu",
132 &sec, &usec) != 2)
133 goto out;
134 idt.start_time.tv_sec = sec;
135 idt.start_time.tv_usec = usec;
136 }
137 if (!conf.writer_given) {
138 wng = setup_default_wng();
139 ret = 1;
140 goto out;
141 }
142 wng = wng_new(conf.writer_given);
143 for (i = 0; i < conf.writer_given; i++) {
144 ret = check_writer_arg(conf.writer_arg[i]);
145 if (ret < 0)
146 goto out;
147 wng->writer_nodes[i].writer = &writers[ret];
148 }
149 ret = 1;
150 out:
151 if (ret > 0)
152 return wng;
153 free(wng);
154 return NULL;
155 }
156
157 static void idt_error_handler(struct task *t)
158 {
159 PARA_ERROR_LOG("task %p, ret: %d\n", t, t->ret);
160 int ret;
161 unregister_task(t);
162 wng->buf = sit.buf;
163 wng->loaded = &sit.loaded;
164 ret = wng_open(wng);
165 if (ret < 0) {
166 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
167 exit(EXIT_FAILURE);
168 }
169 }
170
171 static void cwt_error_handler(struct task *t)
172 {
173 PARA_ERROR_LOG("task %p, ret: %d\n", t, t->ret);
174 if (t->ret < 0) {
175 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret));
176 if (t->ret != -E_NO_WAV_HEADER && t->ret != -E_PRE_EOF)
177 exit(EXIT_FAILURE);
178 if (t->ret == -E_PRE_EOF) {
179 conf.channels_arg = cwt.channels;
180 conf.sample_rate_arg = cwt.sample_rate;
181 }
182 }
183 unregister_task(t);
184 idt.task.pre_select = initial_delay_pre_select;
185 idt.task.private_data = &idt;
186 idt.task.error_handler = idt_error_handler;
187 idt.task.flags = PRE_EOF_IS_ERROR;
188 register_task(&idt.task);
189 }
190
191 static void stdin_error_handler(struct task *t)
192 {
193 unregister_task(t);
194 PARA_INFO_LOG("task %p, ret: %d\n", t, t->ret);
195 if (t->ret < 0)
196 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret));
197 wng->eof = 1;
198 }
199
200 int main(int argc, char *argv[])
201 {
202 int ret = -E_WRITE_SYNTAX;
203 struct sched s;
204
205 cmdline_parser(argc, argv, &conf);
206 wng = check_args();
207 if (!wng)
208 goto out;
209 init_supported_writers();
210 init_sched();
211
212 sit.bufsize = 16 * 1024,
213 sit.buf = para_malloc(16 * 1024),
214 sit.loaded = 0,
215 sit.task.pre_select = stdin_pre_select;
216 sit.task.post_select = stdin_post_select;
217 sit.task.error_handler = stdin_error_handler;
218 sit.task.flags = POST_EOF_IS_ERROR;
219 sit.task.private_data = &sit;
220 register_task(&sit.task);
221
222 cwt.task.pre_select = check_wav_pre_select;
223 cwt.task.private_data = &cwt;
224 cwt.task.error_handler = cwt_error_handler;
225 cwt.buf = sit.buf;
226 cwt.loaded = &sit.loaded;
227 cwt.task.flags = PRE_EOF_IS_ERROR;
228 register_task(&cwt.task);
229
230 s.default_timeout.tv_sec = 1;
231 s.default_timeout.tv_usec = 0;
232 ret = sched(&s);
233
234 out:
235 if (ret < 0) {
236 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
237 ret = EXIT_FAILURE;
238 } else
239 ret = EXIT_SUCCESS;
240 return ret;
241 }