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 */
11 #include "write.cmdline.h"
16 #include "write_common.h"
23 * check if given buffer contains a valid wave header
25 struct check_wav_task
{
26 /** the buffer to check */
28 /** number of bytes loaded in \a buf */
30 /** non-zero if end of file was reached */
32 /** number of channels specified in wav header given by \a buf */
34 /** samplerate specified in wav header given by \a buf */
36 /** the task structure for this task */
41 * 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 */
50 static struct write_args_info conf
;
51 static struct stdin_task sit
;
52 static struct check_wav_task cwt
;
53 static struct initial_delay_task idt
;
54 static struct writer_node_group
*wng
;
56 /** length of a standard wav header */
57 #define WAV_HEADER_LEN 44
60 * test if audio buffer contains a valid wave header
62 * \return If not, return -E_NO_WAV_HEADER, otherwise, return zero. If
63 * there is less than WAV_HEADER_LEN bytes awailable, return one.
65 static void check_wav_pre_select(__a_unused
struct sched
*s
, struct task
*t
)
67 struct check_wav_task
*wt
= t
->private_data
;
70 if (*wt
->loaded
< WAV_HEADER_LEN
) {
71 t
->ret
= *wt
->eof
? -E_PREMATURE_END
: 1;
75 wt
->samplerate
= 44100;
76 a
= (unsigned char*)wt
->buf
;
77 t
->ret
= -E_NO_WAV_HEADER
;
78 if (a
[0] != 'R' || a
[1] != 'I' || a
[2] != 'F' || a
[3] != 'F')
80 wt
->channels
= (unsigned) a
[22];
81 wt
->samplerate
= a
[24] + (a
[25] << 8) + (a
[26] << 16) + (a
[27] << 24);
82 *wt
->loaded
-= WAV_HEADER_LEN
;
83 memmove(wt
->buf
, wt
->buf
+ WAV_HEADER_LEN
, *wt
->loaded
);
84 t
->ret
= -E_WAV_HEADER_SUCCESS
;
85 PARA_INFO_LOG("channels: %d, sample rate: %d\n", wt
->channels
, wt
->samplerate
);
88 static void initial_delay_pre_select(struct sched
*s
, struct task
*t
)
90 struct initial_delay_task
*dt
= t
->private_data
;
94 if (!dt
->start_time
.tv_sec
&& !dt
->start_time
.tv_usec
)
96 t
->ret
= -E_DELAY_TIMEOUT
;
97 if (tv_diff(now
, &dt
->start_time
, &diff
) > 0)
100 if (tv_diff(&s
->timeout
, &diff
, NULL
) > 0)
104 INIT_STDERR_LOGGING(conf
.loglevel_arg
)
106 static struct writer_node_group
*check_args(void)
108 int i
, ret
= -E_WRITE_SYNTAX
;
109 struct writer_node_group
*g
= NULL
;
111 if (conf
.list_writers_given
) {
114 char *tmp
= make_message("%s%s%s",
121 fprintf(stderr
, "%s\n", msg
);
125 if (conf
.start_time_given
) {
126 long unsigned sec
, usec
;
127 if (sscanf(conf
.start_time_arg
, "%lu:%lu",
130 idt
.start_time
.tv_sec
= sec
;
131 idt
.start_time
.tv_usec
= usec
;
133 if (!conf
.writer_given
) {
134 g
= setup_default_wng();
138 g
= wng_new(conf
.writer_given
);
139 ret
= -E_WRITE_SYNTAX
;
140 for (i
= 0; i
< conf
.writer_given
; i
++) {
142 g
->writer_nodes
[i
].conf
= check_writer_arg(
143 conf
.writer_arg
[i
], &writer_num
);
144 if (!g
->writer_nodes
[i
].conf
)
146 g
->writer_nodes
[i
].writer
= &writers
[writer_num
];
156 static void wng_event_handler(struct task
*t
)
158 struct writer_node_group
*g
= t
->private_data
;
160 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t
->ret
));
166 static void idt_event_handler(struct task
*t
)
170 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t
->ret
));
173 wng
->loaded
= &sit
.loaded
;
174 wng
->input_eof
= &sit
.eof
;
175 wng
->task
.event_handler
= wng_event_handler
;
176 wng
->channels
= &cwt
.channels
;
177 wng
->samplerate
= &cwt
.samplerate
;
180 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
185 static void cwt_event_handler(struct task
*t
)
187 if (t
->ret
!= -E_NO_WAV_HEADER
&& t
->ret
!= -E_WAV_HEADER_SUCCESS
) {
188 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t
->ret
));
191 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t
->ret
));
193 // if (t->ret == -E_WAV_HEADER_SUCCESS) {
194 // conf.channels_arg = cwt.channels;
195 // conf.sample_rate_arg = cwt.sample_rate;
197 idt
.task
.pre_select
= initial_delay_pre_select
;
198 idt
.task
.private_data
= &idt
;
199 idt
.task
.event_handler
= idt_event_handler
;
200 sprintf(idt
.task
.status
, "initial_delay");
201 register_task(&idt
.task
);
205 * para_write's main function
207 * \param argc the usual argument counter
208 * \param argv the usual argument vector
210 * It registers the stdin task, the check_wav_task, the task for initial delay
211 * and all tasks for actually writing out the stream.
213 * \return \p EXIT_SUCCESS or EXIT_FAILURE
215 int main(int argc
, char *argv
[])
217 int ret
= -E_WRITE_SYNTAX
;
220 write_cmdline_parser(argc
, argv
, &conf
);
221 HANDLE_VERSION_FLAG("write", conf
);
222 init_supported_writers();
227 stdin_set_defaults(&sit
);
228 if (conf
.bufsize_given
)
229 sit
.bufsize
= conf
.bufsize_arg
;
230 sit
.buf
= para_malloc(sit
.bufsize
),
231 register_task(&sit
.task
);
233 cwt
.task
.pre_select
= check_wav_pre_select
;
234 cwt
.task
.private_data
= &cwt
;
235 cwt
.task
.event_handler
= cwt_event_handler
;
237 cwt
.loaded
= &sit
.loaded
;
239 sprintf(cwt
.task
.status
, "check wav");
240 register_task(&cwt
.task
);
242 s
.default_timeout
.tv_sec
= 1;
243 s
.default_timeout
.tv_usec
= 0;
248 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));