introduce input_eof and ouput_eof
[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 sample_rate;
38 struct task task;
39 };
40
41 struct initial_delay_task {
42 struct timeval start_time;
43 struct task task;
44 };
45
46 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(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 a = (unsigned char*)cwt->buf;
70 t->ret = -E_NO_WAV_HEADER;
71 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F')
72 return;
73 cwt->channels = (unsigned) a[22];
74 cwt->sample_rate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
75 *cwt->loaded -= WAV_HEADER_LEN;
76 memmove(cwt->buf, cwt->buf + WAV_HEADER_LEN, *cwt->loaded);
77 t->ret = -E_WAV_HEADER_SUCCESS;
78 PARA_INFO_LOG("channels: %d, sample_rate: %d\n", cwt->channels, cwt->sample_rate);
79 }
80
81 static void initial_delay_pre_select(struct sched *s, struct task *t)
82 {
83 struct initial_delay_task *idt = t->private_data;
84 struct timeval diff;
85
86 t->ret = -E_NO_DELAY;
87 if (!idt->start_time.tv_sec && !idt->start_time.tv_usec)
88 return;
89 t->ret = -E_DELAY_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 sprintf(wng->writer_nodes[i].task.status, "%s",
149 writer_names[ret]);
150 }
151 ret = 1;
152 out:
153 if (ret > 0)
154 return wng;
155 free(wng);
156 return NULL;
157 }
158
159 static void idt_event_handler(struct task *t)
160 {
161 int ret;
162
163 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
164 unregister_task(t);
165 wng->buf = sit.buf;
166 wng->loaded = &sit.loaded;
167 wng->input_eof = &sit.eof;
168 ret = wng_open(wng);
169 if (ret < 0) {
170 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
171 exit(EXIT_FAILURE);
172 }
173 }
174
175 static void cwt_event_handler(struct task *t)
176 {
177 if (t->ret != -E_NO_WAV_HEADER && t->ret != -E_WAV_HEADER_SUCCESS) {
178 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret));
179 exit(EXIT_FAILURE);
180 }
181 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
182 unregister_task(t);
183 if (t->ret == -E_WAV_HEADER_SUCCESS) {
184 conf.channels_arg = cwt.channels;
185 conf.sample_rate_arg = cwt.sample_rate;
186 }
187 idt.task.pre_select = initial_delay_pre_select;
188 idt.task.private_data = &idt;
189 idt.task.event_handler = idt_event_handler;
190 sprintf(idt.task.status, "initial_delay");
191 register_task(&idt.task);
192 }
193
194 int main(int argc, char *argv[])
195 {
196 int ret = -E_WRITE_SYNTAX;
197 struct sched s;
198
199 cmdline_parser(argc, argv, &conf);
200 wng = check_args();
201 if (!wng)
202 goto out;
203 init_supported_writers();
204 init_sched();
205
206 stdin_set_defaults(&sit);
207 sit.buf = para_malloc(sit.bufsize),
208 register_task(&sit.task);
209
210 cwt.task.pre_select = check_wav_pre_select;
211 cwt.task.private_data = &cwt;
212 cwt.task.event_handler = cwt_event_handler;
213 cwt.buf = sit.buf;
214 cwt.loaded = &sit.loaded;
215 cwt.eof = &sit.eof;
216 sprintf(cwt.task.status, "check wav");
217 register_task(&cwt.task);
218
219 s.default_timeout.tv_sec = 1;
220 s.default_timeout.tv_usec = 0;
221 ret = sched(&s);
222
223 out:
224 if (ret < 0) {
225 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
226 ret = EXIT_FAILURE;
227 } else
228 ret = EXIT_SUCCESS;
229 return ret;
230 }