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