file_write.c: make file_write_parse_config() static and change arg to const char*
[paraslash.git] / write.c
1 /*
2  * Copyright (C) 2005-2007 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 INIT_STDERR_LOGGING(conf.loglevel_arg)
117
118 static struct writer_node_group *check_args(void)
119 {
120         int i, ret = -E_WRITE_SYNTAX;
121         struct writer_node_group *g = NULL;
122
123         if (conf.list_writers_given) {
124                 char *msg = NULL;
125                 FOR_EACH_WRITER(i) {
126                         char *tmp = make_message("%s%s%s",
127                                 i? msg : "",
128                                 i? " " : "",
129                                 writer_names[i]);
130                         free(msg);
131                         msg = tmp;
132                 }
133                 fprintf(stderr, "%s\n", msg);
134                 free(msg);
135                 exit(EXIT_SUCCESS);
136         }
137         if (conf.start_time_given) {
138                 long unsigned sec, usec;
139                 if (sscanf(conf.start_time_arg, "%lu:%lu",
140                                 &sec, &usec) != 2)
141                         goto out;
142                 idt.start_time.tv_sec = sec;
143                 idt.start_time.tv_usec = usec;
144         }
145         if (!conf.writer_given) {
146                 g = setup_default_wng();
147                 ret = 1;
148                 goto out;
149         }
150         g = wng_new(conf.writer_given);
151         ret = -E_WRITE_SYNTAX;
152         for (i = 0; i < conf.writer_given; i++) {
153                 int writer_num;
154                 g->writer_nodes[i].conf = check_writer_arg(
155                         conf.writer_arg[i], &writer_num);
156                 if (!g->writer_nodes[i].conf)
157                         goto out;
158                 g->writer_nodes[i].writer = &writers[writer_num];
159         }
160         ret = 1;
161 out:
162         if (ret > 0)
163                 return g;
164         free(g);
165         return NULL;
166 }
167
168 static void wng_event_handler(struct task *t)
169 {
170         struct writer_node_group *g = t->private_data;
171
172         PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
173         unregister_task(t);
174         wng_close(g);
175 }
176
177
178 static void idt_event_handler(struct task *t)
179 {
180         int ret;
181
182         PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
183         unregister_task(t);
184         wng->buf = sit.buf;
185         wng->loaded = &sit.loaded;
186         wng->input_eof = &sit.eof;
187         wng->task.event_handler = wng_event_handler;
188         wng->channels = &cwt.channels;
189         wng->samplerate = &cwt.samplerate;
190         ret = wng_open(wng);
191         if (ret < 0) {
192                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
193                 exit(EXIT_FAILURE);
194         }
195 }
196
197 static void cwt_event_handler(struct task *t)
198 {
199         if (t->ret != -E_NO_WAV_HEADER && t->ret != -E_WAV_HEADER_SUCCESS) {
200                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t->ret));
201                 exit(EXIT_FAILURE);
202         }
203         PARA_INFO_LOG("%s\n", PARA_STRERROR(-t->ret));
204         unregister_task(t);
205 //      if (t->ret == -E_WAV_HEADER_SUCCESS) {
206 //              conf.channels_arg = cwt.channels;
207 //              conf.sample_rate_arg = cwt.sample_rate;
208 //      }
209         idt.task.pre_select = initial_delay_pre_select;
210         idt.task.private_data = &idt;
211         idt.task.event_handler = idt_event_handler;
212         sprintf(idt.task.status, "initial_delay");
213         register_task(&idt.task);
214 }
215
216 /**
217  * para_write's main function
218  *
219  * It registers the stdin task, the check_wav_task, the task for initial delay
220  * and all tasks for actually writing out the stream.
221  *
222  */
223 int main(int argc, char *argv[])
224 {
225         int ret = -E_WRITE_SYNTAX;
226         struct sched s;
227
228         write_cmdline_parser(argc, argv, &conf);
229         init_supported_writers();
230
231         wng = check_args();
232         if (!wng)
233                 goto out;
234         stdin_set_defaults(&sit);
235         if (conf.bufsize_given)
236                 sit.bufsize = conf.bufsize_arg;
237         sit.buf = para_malloc(sit.bufsize),
238         register_task(&sit.task);
239
240         cwt.task.pre_select = check_wav_pre_select;
241         cwt.task.private_data = &cwt;
242         cwt.task.event_handler = cwt_event_handler;
243         cwt.buf = sit.buf;
244         cwt.loaded = &sit.loaded;
245         cwt.eof = &sit.eof;
246         sprintf(cwt.task.status, "check wav");
247         register_task(&cwt.task);
248
249         s.default_timeout.tv_sec = 1;
250         s.default_timeout.tv_usec = 0;
251         ret = sched(&s);
252
253 out:
254         if (ret < 0) {
255                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
256                 ret = EXIT_FAILURE;
257         } else
258                 ret = EXIT_SUCCESS;
259         return ret;
260 }