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