Use symbolic names for loglevels and clean up the ggo mess.
[paraslash.git] / write.c
1 /*
2  * Copyright (C) 2005-2009 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 "ggo.h"
18 #include "stdin.h"
19 #include "write.h"
20 #include "write_common.h"
21 #include "fd.h"
22 #include "error.h"
23
24 INIT_WRITE_ERRLISTS;
25
26 /** Check if given buffer contains a valid wave header. */
27 struct check_wav_task {
28         /** The buffer to check. */
29         char *buf;
30         /** Number of bytes loaded in \a buf. */
31         size_t *loaded;
32         /** Non-zero if an error occurred or end of file was reached. */
33         int *input_error;
34         /** Number of channels specified in wav header given by \a buf. */
35         unsigned channels;
36         /** Sample rate specified in wav header given by \a buf. */
37         unsigned samplerate;
38         /** The task structure used by the scheduler. */
39         struct task task;
40 };
41
42 /** Delay writing until given time. */
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
52 static struct stdin_task sit;
53
54 static struct check_wav_task the_check_wav_task;
55 static struct initial_delay_task the_initial_delay_task;
56
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 available, return one.
67  */
68 static void check_wav_pre_select(__a_unused struct sched *s, struct task *t)
69 {
70         struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
71         unsigned char *a;
72         int ret;
73
74         if (*cwt->loaded < WAV_HEADER_LEN) {
75                 if (*cwt->input_error < 0)
76                         t->error = *cwt->input_error;
77                 return;
78         }
79         cwt->channels = 2;
80         cwt->samplerate = 44100;
81         a = (unsigned char*)cwt->buf;
82         if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F') {
83                 PARA_NOTICE_LOG("wav header not found\n");
84                 t->error = -E_NO_WAV_HEADER;
85                 goto out;
86         }
87         cwt->channels = (unsigned) a[22];
88         cwt->samplerate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
89         *cwt->loaded -= WAV_HEADER_LEN;
90         memmove(cwt->buf, cwt->buf + WAV_HEADER_LEN, *cwt->loaded);
91         t->error = -E_WAV_HEADER_SUCCESS;
92         PARA_INFO_LOG("channels: %d, sample rate: %d\n", cwt->channels, cwt->samplerate);
93 out:
94         wng->channels = &cwt->channels;
95         wng->samplerate = &cwt->samplerate;
96         ret = wng_open(wng);
97         if (ret < 0)
98                 t->error = ret;
99         s->timeout.tv_sec = 0;
100         s->timeout.tv_usec = 1;
101 }
102
103 static void initial_delay_pre_select(struct sched *s, struct task *t)
104 {
105         struct initial_delay_task *idt = container_of(t, struct initial_delay_task, task);
106         struct timeval diff;
107
108         if (!idt->start_time.tv_sec && !idt->start_time.tv_usec) {
109                 t->error = -E_NO_DELAY;
110                 goto register_check_wav;
111         }
112         if (tv_diff(now, &idt->start_time, &diff) > 0) {
113                 t->error = -E_DELAY_TIMEOUT;
114                 goto register_check_wav;
115         }
116         if (tv_diff(&s->timeout , &diff, NULL) > 0)
117                 s->timeout = diff;
118         return;
119 register_check_wav:
120         register_task(&the_check_wav_task.task);
121         s->timeout.tv_sec = 0;
122         s->timeout.tv_usec = 1;
123 }
124
125 static int loglevel;
126 INIT_STDERR_LOGGING(loglevel)
127
128 static struct writer_node_group *check_args(void)
129 {
130         int i, ret = -E_WRITE_SYNTAX;
131         struct writer_node_group *g = NULL;
132         struct initial_delay_task *idt = &the_initial_delay_task;
133
134         loglevel = get_loglevel_by_name(conf.loglevel_arg);
135         if (conf.start_time_given) {
136                 long unsigned sec, usec;
137                 if (sscanf(conf.start_time_arg, "%lu:%lu",
138                                 &sec, &usec) != 2)
139                         goto out;
140                 idt->start_time.tv_sec = sec;
141                 idt->start_time.tv_usec = usec;
142         }
143         if (!conf.writer_given) {
144                 g = setup_default_wng();
145                 ret = 1;
146                 goto out;
147         }
148         g = wng_new(conf.writer_given);
149         ret = -E_WRITE_SYNTAX;
150         for (i = 0; i < conf.writer_given; i++) {
151                 int writer_num;
152                 g->writer_nodes[i].conf = check_writer_arg(
153                         conf.writer_arg[i], &writer_num);
154                 if (!g->writer_nodes[i].conf)
155                         goto out;
156                 g->writer_nodes[i].writer = &writers[writer_num];
157         }
158         ret = 1;
159 out:
160         if (ret > 0)
161                 return g;
162         free(g);
163         return NULL;
164 }
165
166 __noreturn static void print_help_and_die(void)
167 {
168         int d = conf.detailed_help_given;
169         const char **p = d? write_args_info_detailed_help
170                 : write_args_info_help;
171
172         printf_or_die("%s\n\n", WRITE_CMDLINE_PARSER_PACKAGE "-"
173                 WRITE_CMDLINE_PARSER_VERSION);
174         printf_or_die("%s\n\n", write_args_info_usage);
175         for (; *p; p++)
176                 printf_or_die("%s\n", *p);
177         print_writer_helps(d);
178         exit(0);
179 }
180
181 /**
182  * Para_write's main function.
183  *
184  * \param argc The usual argument counter.
185  * \param argv The usual argument vector.
186  *
187  * It registers the stdin task, the check_wav_task, the task for initial delay
188  * and all tasks for actually writing out the stream.
189  *
190  * \return \p EXIT_SUCCESS or EXIT_FAILURE
191  */
192 int main(int argc, char *argv[])
193 {
194         int ret = -E_WRITE_SYNTAX;
195         static struct sched s;
196         struct check_wav_task *cwt = &the_check_wav_task;
197         struct initial_delay_task *idt = &the_initial_delay_task;
198
199         init_supported_writers();
200         write_cmdline_parser(argc, argv, &conf);
201         HANDLE_VERSION_FLAG("write", conf);
202         if (conf.help_given || conf.detailed_help_given)
203                 print_help_and_die();
204
205         wng = check_args();
206         if (!wng)
207                 goto out;
208         stdin_set_defaults(&sit);
209         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
210         if (conf.bufsize_arg < 0)
211                 goto out;
212         if (conf.bufsize_arg >= INT_MAX / 1024)
213                 goto out;
214         sit.bufsize = conf.bufsize_arg * 1024;
215         sit.buf = para_malloc(sit.bufsize);
216
217         wng->buf = sit.buf;
218         wng->loaded = &sit.loaded;
219         wng->input_error = &sit.task.error;
220
221         register_task(&sit.task);
222
223         cwt->buf = sit.buf;
224         cwt->loaded = &sit.loaded;
225         cwt->input_error = &sit.task.error;
226         sprintf(cwt->task.status, "check wav");
227         cwt->task.pre_select = check_wav_pre_select;
228
229         idt->task.pre_select = initial_delay_pre_select;
230         sprintf(idt->task.status, "initial_delay");
231         register_task(&idt->task);
232
233         s.default_timeout.tv_sec = 10;
234         s.default_timeout.tv_usec = 0;
235         ret = schedule(&s);
236         wng_close(wng);
237 out:
238         if (ret < 0) {
239                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
240                 exit(EXIT_FAILURE);
241         }
242         exit(EXIT_SUCCESS);
243 }