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"
25 /** Check if given buffer contains a valid wave header. */
26 struct check_wav_task
{
27 /** The buffer to check. */
29 /** Number of bytes loaded in \a buf. */
31 /** Non-zero if end of file was reached. */
33 /** Number of channels specified in wav header given by \a buf. */
35 /** Samplerate specified in wav header given by \a buf. */
37 /** The task structure for this task. */
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. */
49 static struct write_args_info conf
;
50 static struct stdin_task sit
;
51 static struct check_wav_task cwt
;
52 static struct initial_delay_task idt
;
53 static struct writer_node_group
*wng
;
55 /** Length of a standard wav header. */
56 #define WAV_HEADER_LEN 44
59 * Test if audio buffer contains a valid wave header.
61 * \return If not, return -E_NO_WAV_HEADER, otherwise, return zero. If
62 * there is less than WAV_HEADER_LEN bytes awailable, return one.
64 static void check_wav_pre_select(__a_unused
struct sched
*s
, struct task
*t
)
66 struct check_wav_task
*wt
= t
->private_data
;
69 if (*wt
->loaded
< WAV_HEADER_LEN
) {
70 t
->ret
= *wt
->eof
? -E_PREMATURE_END
: 1;
74 wt
->samplerate
= 44100;
75 a
= (unsigned char*)wt
->buf
;
76 t
->ret
= -E_NO_WAV_HEADER
;
77 if (a
[0] != 'R' || a
[1] != 'I' || a
[2] != 'F' || a
[3] != 'F')
79 wt
->channels
= (unsigned) a
[22];
80 wt
->samplerate
= a
[24] + (a
[25] << 8) + (a
[26] << 16) + (a
[27] << 24);
81 *wt
->loaded
-= WAV_HEADER_LEN
;
82 memmove(wt
->buf
, wt
->buf
+ WAV_HEADER_LEN
, *wt
->loaded
);
83 t
->ret
= -E_WAV_HEADER_SUCCESS
;
84 PARA_INFO_LOG("channels: %d, sample rate: %d\n", wt
->channels
, wt
->samplerate
);
87 static void initial_delay_pre_select(struct sched
*s
, struct task
*t
)
89 struct initial_delay_task
*dt
= t
->private_data
;
93 if (!dt
->start_time
.tv_sec
&& !dt
->start_time
.tv_usec
)
95 t
->ret
= -E_DELAY_TIMEOUT
;
96 if (tv_diff(now
, &dt
->start_time
, &diff
) > 0)
99 if (tv_diff(&s
->timeout
, &diff
, NULL
) > 0)
103 INIT_STDERR_LOGGING(conf
.loglevel_arg
)
105 static struct writer_node_group
*check_args(void)
107 int i
, ret
= -E_WRITE_SYNTAX
;
108 struct writer_node_group
*g
= NULL
;
110 if (conf
.list_writers_given
) {
113 char *tmp
= make_message("%s%s%s",
120 fprintf(stderr
, "%s\n", msg
);
124 if (conf
.start_time_given
) {
125 long unsigned sec
, usec
;
126 if (sscanf(conf
.start_time_arg
, "%lu:%lu",
129 idt
.start_time
.tv_sec
= sec
;
130 idt
.start_time
.tv_usec
= usec
;
132 if (!conf
.writer_given
) {
133 g
= setup_default_wng();
137 g
= wng_new(conf
.writer_given
);
138 ret
= -E_WRITE_SYNTAX
;
139 for (i
= 0; i
< conf
.writer_given
; i
++) {
141 g
->writer_nodes
[i
].conf
= check_writer_arg(
142 conf
.writer_arg
[i
], &writer_num
);
143 if (!g
->writer_nodes
[i
].conf
)
145 g
->writer_nodes
[i
].writer
= &writers
[writer_num
];
155 static void wng_event_handler(struct task
*t
)
157 struct writer_node_group
*g
= t
->private_data
;
159 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t
->ret
));
165 static void idt_event_handler(struct task
*t
)
169 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t
->ret
));
172 wng
->loaded
= &sit
.loaded
;
173 wng
->input_eof
= &sit
.eof
;
174 wng
->task
.event_handler
= wng_event_handler
;
175 wng
->channels
= &cwt
.channels
;
176 wng
->samplerate
= &cwt
.samplerate
;
179 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
184 static void cwt_event_handler(struct task
*t
)
186 if (t
->ret
!= -E_NO_WAV_HEADER
&& t
->ret
!= -E_WAV_HEADER_SUCCESS
) {
187 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-t
->ret
));
190 PARA_INFO_LOG("%s\n", PARA_STRERROR(-t
->ret
));
192 // if (t->ret == -E_WAV_HEADER_SUCCESS) {
193 // conf.channels_arg = cwt.channels;
194 // conf.sample_rate_arg = cwt.sample_rate;
196 idt
.task
.pre_select
= initial_delay_pre_select
;
197 idt
.task
.private_data
= &idt
;
198 idt
.task
.event_handler
= idt_event_handler
;
199 sprintf(idt
.task
.status
, "initial_delay");
200 register_task(&idt
.task
);
204 * Para_write's main function.
206 * \param argc The usual argument counter.
207 * \param argv The usual argument vector.
209 * It registers the stdin task, the check_wav_task, the task for initial delay
210 * and all tasks for actually writing out the stream.
212 * \return \p EXIT_SUCCESS or EXIT_FAILURE
214 int main(int argc
, char *argv
[])
216 int ret
= -E_WRITE_SYNTAX
;
219 write_cmdline_parser(argc
, argv
, &conf
);
220 HANDLE_VERSION_FLAG("write", conf
);
221 init_supported_writers();
226 stdin_set_defaults(&sit
);
227 if (conf
.bufsize_given
)
228 sit
.bufsize
= conf
.bufsize_arg
;
229 sit
.buf
= para_malloc(sit
.bufsize
),
230 register_task(&sit
.task
);
232 cwt
.task
.pre_select
= check_wav_pre_select
;
233 cwt
.task
.private_data
= &cwt
;
234 cwt
.task
.event_handler
= cwt_event_handler
;
236 cwt
.loaded
= &sit
.loaded
;
238 sprintf(cwt
.task
.status
, "check wav");
239 register_task(&cwt
.task
);
241 s
.default_timeout
.tv_sec
= 1;
242 s
.default_timeout
.tv_usec
= 0;
247 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));