9b7a92113e69ff236e08ce235ca306c9c410fd07
[paraslash.git] / write.c
1 /*
2 * Copyright (C) 2005-2008 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 INIT_STDERR_LOGGING(conf.loglevel_arg)
126
127 static struct writer_node_group *check_args(void)
128 {
129 int i, ret = -E_WRITE_SYNTAX;
130 struct writer_node_group *g = NULL;
131 struct initial_delay_task *idt = &the_initial_delay_task;
132
133 if (conf.start_time_given) {
134 long unsigned sec, usec;
135 if (sscanf(conf.start_time_arg, "%lu:%lu",
136 &sec, &usec) != 2)
137 goto out;
138 idt->start_time.tv_sec = sec;
139 idt->start_time.tv_usec = usec;
140 }
141 if (!conf.writer_given) {
142 g = setup_default_wng();
143 ret = 1;
144 goto out;
145 }
146 g = wng_new(conf.writer_given);
147 ret = -E_WRITE_SYNTAX;
148 for (i = 0; i < conf.writer_given; i++) {
149 int writer_num;
150 g->writer_nodes[i].conf = check_writer_arg(
151 conf.writer_arg[i], &writer_num);
152 if (!g->writer_nodes[i].conf)
153 goto out;
154 g->writer_nodes[i].writer = &writers[writer_num];
155 }
156 ret = 1;
157 out:
158 if (ret > 0)
159 return g;
160 free(g);
161 return NULL;
162 }
163
164 __noreturn static void print_help_and_die(void)
165 {
166 int d = conf.detailed_help_given;
167 const char **p = d? write_args_info_detailed_help
168 : write_args_info_help;
169
170 printf_or_die("%s\n\n", WRITE_CMDLINE_PARSER_PACKAGE "-"
171 WRITE_CMDLINE_PARSER_VERSION);
172 printf_or_die("%s\n\n", write_args_info_usage);
173 for (; *p; p++)
174 printf_or_die("%s\n", *p);
175 print_writer_helps(d);
176 exit(0);
177 }
178
179 /**
180 * Para_write's main function.
181 *
182 * \param argc The usual argument counter.
183 * \param argv The usual argument vector.
184 *
185 * It registers the stdin task, the check_wav_task, the task for initial delay
186 * and all tasks for actually writing out the stream.
187 *
188 * \return \p EXIT_SUCCESS or EXIT_FAILURE
189 */
190 int main(int argc, char *argv[])
191 {
192 int ret = -E_WRITE_SYNTAX;
193 static struct sched s;
194 struct check_wav_task *cwt = &the_check_wav_task;
195 struct initial_delay_task *idt = &the_initial_delay_task;
196
197 init_supported_writers();
198 write_cmdline_parser(argc, argv, &conf);
199 HANDLE_VERSION_FLAG("write", conf);
200 if (conf.help_given || conf.detailed_help_given)
201 print_help_and_die();
202
203 wng = check_args();
204 if (!wng)
205 goto out;
206 stdin_set_defaults(&sit);
207 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
208 if (conf.bufsize_arg < 0)
209 goto out;
210 if (conf.bufsize_arg >= INT_MAX / 1024)
211 goto out;
212 sit.bufsize = conf.bufsize_arg * 1024;
213 sit.buf = para_malloc(sit.bufsize);
214
215 wng->buf = sit.buf;
216 wng->loaded = &sit.loaded;
217 wng->input_error = &sit.task.error;
218
219 register_task(&sit.task);
220
221 cwt->buf = sit.buf;
222 cwt->loaded = &sit.loaded;
223 cwt->input_error = &sit.task.error;
224 sprintf(cwt->task.status, "check wav");
225 cwt->task.pre_select = check_wav_pre_select;
226
227 idt->task.pre_select = initial_delay_pre_select;
228 sprintf(idt->task.status, "initial_delay");
229 register_task(&idt->task);
230
231 s.default_timeout.tv_sec = 10;
232 s.default_timeout.tv_usec = 0;
233 ret = schedule(&s);
234 wng_close(wng);
235 out:
236 if (ret < 0) {
237 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
238 exit(EXIT_FAILURE);
239 }
240 exit(EXIT_SUCCESS);
241 }