2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file write.c Paraslash's standalone wav/raw player */
14 #include "write.cmdline.h"
19 #include "write_common.h"
26 * check if given buffer contains a valid wave header
28 struct check_wav_task
{
29 /** the buffer to check */
31 /** number of bytes loaded in \a buf */
33 /** non-zero if end of file was reached */
35 /** number of channels specified in wav header given by \a buf */
37 /** samplerate specified in wav header given by \a buf */
39 /** the task structure for this task */
44 * delay writing until given time
46 struct initial_delay_task
{
47 /** the time the first data should be written out */
48 struct timeval start_time
;
49 /** the task structure for this task */
53 static struct write_args_info conf
;
54 static struct stdin_task sit
;
55 static struct check_wav_task cwt
;
56 static struct initial_delay_task idt
;
57 static struct writer_node_group
*wng
;
59 /** length of a standard wav header */
60 #define WAV_HEADER_LEN 44
63 * test if audio buffer contains a valid wave header
65 * \return If not, return -E_NO_WAV_HEADER, otherwise, return zero. If
66 * there is less than WAV_HEADER_LEN bytes awailable, return one.
68 static void check_wav_pre_select(__a_unused
struct sched
*s
, struct task
*t
)
70 struct check_wav_task
*wt
= t
->private_data
;
73 if (*wt
->loaded
< WAV_HEADER_LEN
) {
74 t
->ret
= *wt
->eof
? -E_PREMATURE_END
: 1;
78 wt
->samplerate
= 44100;
79 a
= (unsigned char*)wt
->buf
;
80 t
->ret
= -E_NO_WAV_HEADER
;
81 if (a
[0] != 'R' || a
[1] != 'I' || a
[2] != 'F' || a
[3] != 'F')
83 wt
->channels
= (unsigned) a
[22];
84 wt
->samplerate
= a
[24] + (a
[25] << 8) + (a
[26] << 16) + (a
[27] << 24);
85 *wt
->loaded
-= WAV_HEADER_LEN
;
86 memmove(wt
->buf
, wt
->buf
+ WAV_HEADER_LEN
, *wt
->loaded
);
87 t
->ret
= -E_WAV_HEADER_SUCCESS
;
88 PARA_INFO_LOG("channels: %d, sample rate: %d\n", wt
->channels
, wt
->samplerate
);
91 static void initial_delay_pre_select(struct sched
*s
, struct task
*t
)
93 struct initial_delay_task
*dt
= t
->private_data
;
97 if (!dt
->start_time
.tv_sec
&& !dt
->start_time
.tv_usec
)
99 t
->ret
= -E_DELAY_TIMEOUT
;
100 if (tv_diff(now
, &dt
->start_time
, &diff
) > 0)
103 if (tv_diff(&s
->timeout
, &diff
, NULL
) > 0)
107 INIT_STDERR_LOGGING(conf
.loglevel_arg
)
109 static struct writer_node_group
*check_args(void)
111 int i
, ret
= -E_WRITE_SYNTAX
;
112 struct writer_node_group
*g
= NULL
;
114 if (conf
.list_writers_given
) {
117 char *tmp
= make_message("%s%s%s",
124 fprintf(stderr
, "%s\n", msg
);
128 if (conf
.start_time_given
) {
129 long unsigned sec
, usec
;
130 if (sscanf(conf
.start_time_arg
, "%lu:%lu",
133 idt
.start_time
.tv_sec
= sec
;
134 idt
.start_time
.tv_usec
= usec
;
136 if (!conf
.writer_given
) {
137 g
= setup_default_wng();
141 g
= wng_new(conf
.writer_given
);
142 ret
= -E_WRITE_SYNTAX
;
143 for (i
= 0; i
< conf
.writer_given
; i
++) {
145 g
->writer_nodes
[i
].conf
= check_writer_arg(
146 conf
.writer_arg
[i
], &writer_num
);
147 if (!g
->writer_nodes
[i
].conf
)
149 g
->writer_nodes
[i
].writer
= &writers
[writer_num
];
159 static void wng_event_handler(struct task
*t
)
161 struct writer_node_group
*g
= t
->private_data
;
163 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t
->ret
));
169 static void idt_event_handler(struct task
*t
)
173 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t
->ret
));
176 wng
->loaded
= &sit
.loaded
;
177 wng
->input_eof
= &sit
.eof
;
178 wng
->task
.event_handler
= wng_event_handler
;
179 wng
->channels
= &cwt
.channels
;
180 wng
->samplerate
= &cwt
.samplerate
;
183 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
188 static void cwt_event_handler(struct task
*t
)
190 if (t
->ret
!= -E_NO_WAV_HEADER
&& t
->ret
!= -E_WAV_HEADER_SUCCESS
) {
191 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t
->ret
));
194 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t
->ret
));
196 // if (t->ret == -E_WAV_HEADER_SUCCESS) {
197 // conf.channels_arg = cwt.channels;
198 // conf.sample_rate_arg = cwt.sample_rate;
200 idt
.task
.pre_select
= initial_delay_pre_select
;
201 idt
.task
.private_data
= &idt
;
202 idt
.task
.event_handler
= idt_event_handler
;
203 sprintf(idt
.task
.status
, "initial_delay");
204 register_task(&idt
.task
);
208 * para_write's main function
210 * \param argc the usual argument counter
211 * \param argv the usual argument vector
213 * It registers the stdin task, the check_wav_task, the task for initial delay
214 * and all tasks for actually writing out the stream.
216 * \return \p EXIT_SUCCESS or EXIT_FAILURE
218 int main(int argc
, char *argv
[])
220 int ret
= -E_WRITE_SYNTAX
;
223 write_cmdline_parser(argc
, argv
, &conf
);
224 HANDLE_VERSION_FLAG("write", conf
);
225 init_supported_writers();
230 stdin_set_defaults(&sit
);
231 if (conf
.bufsize_given
)
232 sit
.bufsize
= conf
.bufsize_arg
;
233 sit
.buf
= para_malloc(sit
.bufsize
),
234 register_task(&sit
.task
);
236 cwt
.task
.pre_select
= check_wav_pre_select
;
237 cwt
.task
.private_data
= &cwt
;
238 cwt
.task
.event_handler
= cwt_event_handler
;
240 cwt
.loaded
= &sit
.loaded
;
242 sprintf(cwt
.task
.status
, "check wav");
243 register_task(&cwt
.task
);
245 s
.default_timeout
.tv_sec
= 1;
246 s
.default_timeout
.tv_usec
= 0;
251 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));