ec27f3d35ac078824e03f33c3ba38572177ef38d
[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 /** \file write.c Paraslash's standalone wav/raw player */
20
21 #include "para.h"
22 #include "string.h"
23 #include "write.cmdline.h"
24 #include "list.h"
25 #include "sched.h"
26 #include "stdin.h"
27 #include "write.h"
28 #include "write_common.h"
29 #include "fd.h"
30 #include "error.h"
31
32 INIT_WRITE_ERRLISTS;
33
34 /**
35 * check if given buffer contains a valid wave header
36 */
37 struct check_wav_task {
38 /** the buffer to check */
39 char *buf;
40 /** number of bytes loaded in \a buf */
41 size_t *loaded;
42 /** non-zero if end of file was reached */
43 int *eof;
44 /** number of channels specified in wav header given by \a buf */
45 unsigned channels;
46 /** samplerate specified in wav header given by \a buf */
47 unsigned samplerate;
48 /** the task structure for this task */
49 struct task task;
50 };
51
52 /**
53 * delay writing until given time
54 */
55 struct initial_delay_task {
56 /** the time the first data should be written out */
57 struct timeval start_time;
58 /** the task structure for this task */
59 struct task task;
60 };
61
62 static struct write_args_info conf;
63 static struct stdin_task sit;
64 static struct check_wav_task cwt;
65 static struct initial_delay_task idt;
66 static struct writer_node_group *wng;
67
68 /** length of a standard wav header */
69 #define WAV_HEADER_LEN 44
70
71 /**
72 * test if audio buffer contains a valid wave header
73 *
74 * \return If not, return -E_NO_WAV_HEADER, otherwise, return zero. If
75 * there is less than WAV_HEADER_LEN bytes awailable, return one.
76 */
77 static void check_wav_pre_select(__a_unused struct sched *s, struct task *t)
78 {
79 struct check_wav_task *wt = t->private_data;
80 unsigned char *a;
81
82 if (*wt->loaded < WAV_HEADER_LEN) {
83 t->ret = *wt->eof? -E_PREMATURE_END : 1;
84 return;
85 }
86 wt->channels = 2;
87 wt->samplerate = 44100;
88 a = (unsigned char*)wt->buf;
89 t->ret = -E_NO_WAV_HEADER;
90 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F')
91 return;
92 wt->channels = (unsigned) a[22];
93 wt->samplerate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
94 *wt->loaded -= WAV_HEADER_LEN;
95 memmove(wt->buf, wt->buf + WAV_HEADER_LEN, *wt->loaded);
96 t->ret = -E_WAV_HEADER_SUCCESS;
97 PARA_INFO_LOG("channels: %d, sample rate: %d\n", wt->channels, wt->samplerate);
98 }
99
100 static void initial_delay_pre_select(struct sched *s, struct task *t)
101 {
102 struct initial_delay_task *dt = t->private_data;
103 struct timeval diff;
104
105 t->ret = -E_NO_DELAY;
106 if (!dt->start_time.tv_sec && !dt->start_time.tv_usec)
107 return;
108 t->ret = -E_DELAY_TIMEOUT;
109 if (tv_diff(now, &dt->start_time, &diff) > 0)
110 return;
111 t->ret = 1;
112 if (tv_diff(&s->timeout , &diff, NULL) > 0)
113 s->timeout = diff;
114 }
115
116 /**
117 * all log messages are written to stderr
118 *
119 * \param ll: loglevel
120 * \param fmt: format string
121 */
122 void para_log(int ll, const char* fmt,...)
123 {
124 va_list argp;
125
126 if (ll < conf.loglevel_arg)
127 return;
128 va_start(argp, fmt);
129 vfprintf(stderr, fmt, argp);
130 va_end(argp);
131 }
132
133 static struct writer_node_group *check_args(void)
134 {
135 int i, ret = -E_WRITE_SYNTAX;
136 struct writer_node_group *g = NULL;
137
138 if (conf.list_writers_given) {
139 char *msg = NULL;
140 FOR_EACH_WRITER(i) {
141 char *tmp = make_message("%s%s%s",
142 i? msg : "",
143 i? " " : "",
144 writer_names[i]);
145 free(msg);
146 msg = tmp;
147 }
148 fprintf(stderr, "%s\n", msg);
149 free(msg);
150 exit(EXIT_SUCCESS);
151 }
152 if (conf.start_time_given) {
153 long unsigned sec, usec;
154 if (sscanf(conf.start_time_arg, "%lu:%lu",
155 &sec, &usec) != 2)
156 goto out;
157 idt.start_time.tv_sec = sec;
158 idt.start_time.tv_usec = usec;
159 }
160 if (!conf.writer_given) {
161 g = setup_default_wng();
162 ret = 1;
163 goto out;
164 }
165 g = wng_new(conf.writer_given);
166 ret = -E_WRITE_SYNTAX;
167 for (i = 0; i < conf.writer_given; i++) {
168 int writer_num;
169 g->writer_nodes[i].conf = check_writer_arg(
170 conf.writer_arg[i], &writer_num);
171 if (!g->writer_nodes[i].conf)
172 goto out;
173 g->writer_nodes[i].writer = &writers[writer_num];
174 }
175 ret = 1;
176 out:
177 if (ret > 0)
178 return g;
179 free(g);
180 return NULL;
181 }
182
183 static void wng_event_handler(struct task *t)
184 {
185 struct writer_node_group *g = t->private_data;
186
187 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
188 unregister_task(t);
189 wng_close(g);
190 }
191
192
193 static void idt_event_handler(struct task *t)
194 {
195 int ret;
196
197 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
198 unregister_task(t);
199 wng->buf = sit.buf;
200 wng->loaded = &sit.loaded;
201 wng->input_eof = &sit.eof;
202 wng->task.event_handler = wng_event_handler;
203 wng->channels = &cwt.channels;
204 wng->samplerate = &cwt.samplerate;
205 ret = wng_open(wng);
206 if (ret < 0) {
207 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
208 exit(EXIT_FAILURE);
209 }
210 }
211
212 static void cwt_event_handler(struct task *t)
213 {
214 if (t->ret != -E_NO_WAV_HEADER && t->ret != -E_WAV_HEADER_SUCCESS) {
215 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret));
216 exit(EXIT_FAILURE);
217 }
218 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
219 unregister_task(t);
220 // if (t->ret == -E_WAV_HEADER_SUCCESS) {
221 // conf.channels_arg = cwt.channels;
222 // conf.sample_rate_arg = cwt.sample_rate;
223 // }
224 idt.task.pre_select = initial_delay_pre_select;
225 idt.task.private_data = &idt;
226 idt.task.event_handler = idt_event_handler;
227 sprintf(idt.task.status, "initial_delay");
228 register_task(&idt.task);
229 }
230
231 /**
232 * para_write's main function
233 *
234 * It registers the stdin task, the check_wav_task, the task for initial delay
235 * and all tasks for actually writing out the stream.
236 *
237 */
238 int main(int argc, char *argv[])
239 {
240 int ret = -E_WRITE_SYNTAX;
241 struct sched s;
242
243 write_cmdline_parser(argc, argv, &conf);
244 init_supported_writers();
245
246 wng = check_args();
247 if (!wng)
248 goto out;
249 stdin_set_defaults(&sit);
250 if (conf.bufsize_given)
251 sit.bufsize = conf.bufsize_arg;
252 sit.buf = para_malloc(sit.bufsize),
253 register_task(&sit.task);
254
255 cwt.task.pre_select = check_wav_pre_select;
256 cwt.task.private_data = &cwt;
257 cwt.task.event_handler = cwt_event_handler;
258 cwt.buf = sit.buf;
259 cwt.loaded = &sit.loaded;
260 cwt.eof = &sit.eof;
261 sprintf(cwt.task.status, "check wav");
262 register_task(&cwt.task);
263
264 s.default_timeout.tv_sec = 1;
265 s.default_timeout.tv_usec = 0;
266 ret = sched(&s);
267
268 out:
269 if (ret < 0) {
270 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
271 ret = EXIT_FAILURE;
272 } else
273 ret = EXIT_SUCCESS;
274 return ret;
275 }