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