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