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