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