fc50040a28c07df608c551a56824188b7a383bf9
[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 int *eof;
36 unsigned channels;
37 unsigned samplerate;
38 struct task task;
39 };
40
41 struct initial_delay_task {
42 struct timeval start_time;
43 struct task task;
44 };
45
46 static struct gengetopt_args_info conf;
47 struct stdin_task sit;
48 struct check_wav_task cwt;
49 struct initial_delay_task idt;
50 static struct writer_node_group *wng;
51
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 awailable, return one.
59 */
60 static void check_wav_pre_select(__a_unused struct sched *s, struct task *t)
61 {
62 struct check_wav_task *cwt = t->private_data;
63 unsigned char *a;
64
65 if (*cwt->loaded < WAV_HEADER_LEN) {
66 t->ret = *cwt->eof? -E_PREMATURE_END : 1;
67 return;
68 }
69 cwt->channels = 2;
70 cwt->samplerate = 44100;
71 a = (unsigned char*)cwt->buf;
72 t->ret = -E_NO_WAV_HEADER;
73 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F')
74 return;
75 cwt->channels = (unsigned) a[22];
76 cwt->samplerate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
77 *cwt->loaded -= WAV_HEADER_LEN;
78 memmove(cwt->buf, cwt->buf + WAV_HEADER_LEN, *cwt->loaded);
79 t->ret = -E_WAV_HEADER_SUCCESS;
80 PARA_INFO_LOG("channels: %d, sample rate: %d\n", cwt->channels, cwt->samplerate);
81 }
82
83 static void initial_delay_pre_select(struct sched *s, struct task *t)
84 {
85 struct initial_delay_task *idt = t->private_data;
86 struct timeval diff;
87
88 t->ret = -E_NO_DELAY;
89 if (!idt->start_time.tv_sec && !idt->start_time.tv_usec)
90 return;
91 t->ret = -E_DELAY_TIMEOUT;
92 if (tv_diff(&s->now, &idt->start_time, &diff) > 0)
93 return;
94 t->ret = 1;
95 if (tv_diff(&s->timeout , &diff, NULL) > 0)
96 s->timeout = diff;
97 }
98
99 void para_log(int ll, const char* fmt,...)
100 {
101 va_list argp;
102
103 if (ll < conf.loglevel_arg)
104 return;
105 va_start(argp, fmt);
106 vfprintf(stderr, fmt, argp);
107 va_end(argp);
108 }
109
110 static struct writer_node_group *check_args(void)
111 {
112 int i, ret = -E_WRITE_SYNTAX;
113 struct writer_node_group *wng = NULL;
114
115 if (conf.list_writers_given) {
116 char *msg = NULL;
117 FOR_EACH_WRITER(i) {
118 char *tmp = make_message("%s%s%s",
119 i? msg : "",
120 i? " " : "",
121 writer_names[i]);
122 free(msg);
123 msg = tmp;
124 }
125 fprintf(stderr, "%s\n", msg);
126 free(msg);
127 exit(EXIT_SUCCESS);
128 }
129 // if (conf.prebuffer_arg < 0 || conf.prebuffer_arg > 100)
130 // goto out;
131 if (conf.start_time_given) {
132 long unsigned sec, usec;
133 if (sscanf(conf.start_time_arg, "%lu:%lu",
134 &sec, &usec) != 2)
135 goto out;
136 idt.start_time.tv_sec = sec;
137 idt.start_time.tv_usec = usec;
138 }
139 if (!conf.writer_given) {
140 wng = setup_default_wng();
141 ret = 1;
142 goto out;
143 }
144 wng = wng_new(conf.writer_given);
145 ret = -E_WRITE_SYNTAX;
146 for (i = 0; i < conf.writer_given; i++) {
147 int writer_num;
148 wng->writer_nodes[i].conf = check_writer_arg(
149 conf.writer_arg[i], &writer_num);
150 if (!wng->writer_nodes[i].conf)
151 goto out;
152 wng->writer_nodes[i].writer = &writers[writer_num];
153 sprintf(wng->writer_nodes[i].task.status, "%s",
154 writer_names[writer_num]);
155 }
156 ret = 1;
157 out:
158 if (ret > 0)
159 return wng;
160 free(wng);
161 return NULL;
162 }
163
164 static void wng_event_handler(struct task *t)
165 {
166 struct writer_node_group *g = t->private_data;
167
168 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
169 unregister_task(t);
170 wng_close(g);
171 }
172
173
174 static void idt_event_handler(struct task *t)
175 {
176 int ret;
177
178 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
179 unregister_task(t);
180 wng->buf = sit.buf;
181 wng->loaded = &sit.loaded;
182 wng->input_eof = &sit.eof;
183 wng->task.event_handler = wng_event_handler;
184 wng->channels = &cwt.channels;
185 wng->samplerate = &cwt.samplerate;
186 ret = wng_open(wng);
187 if (ret < 0) {
188 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
189 exit(EXIT_FAILURE);
190 }
191 }
192
193 static void cwt_event_handler(struct task *t)
194 {
195 if (t->ret != -E_NO_WAV_HEADER && t->ret != -E_WAV_HEADER_SUCCESS) {
196 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret));
197 exit(EXIT_FAILURE);
198 }
199 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
200 unregister_task(t);
201 // if (t->ret == -E_WAV_HEADER_SUCCESS) {
202 // conf.channels_arg = cwt.channels;
203 // conf.sample_rate_arg = cwt.sample_rate;
204 // }
205 idt.task.pre_select = initial_delay_pre_select;
206 idt.task.private_data = &idt;
207 idt.task.event_handler = idt_event_handler;
208 sprintf(idt.task.status, "initial_delay");
209 register_task(&idt.task);
210 }
211
212 int main(int argc, char *argv[])
213 {
214 int ret = -E_WRITE_SYNTAX;
215 struct sched s;
216
217 cmdline_parser(argc, argv, &conf);
218 init_supported_writers();
219 init_sched();
220
221 wng = check_args();
222 if (!wng)
223 goto out;
224 stdin_set_defaults(&sit);
225 if (conf.bufsize_given)
226 sit.bufsize = conf.bufsize_arg;
227 sit.buf = para_malloc(sit.bufsize),
228 register_task(&sit.task);
229
230 cwt.task.pre_select = check_wav_pre_select;
231 cwt.task.private_data = &cwt;
232 cwt.task.event_handler = cwt_event_handler;
233 cwt.buf = sit.buf;
234 cwt.loaded = &sit.loaded;
235 cwt.eof = &sit.eof;
236 sprintf(cwt.task.status, "check wav");
237 register_task(&cwt.task);
238
239 s.default_timeout.tv_sec = 1;
240 s.default_timeout.tv_usec = 0;
241 ret = sched(&s);
242
243 out:
244 if (ret < 0) {
245 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
246 ret = EXIT_FAILURE;
247 } else
248 ret = EXIT_SUCCESS;
249 return ret;
250 }