-static struct check_wav_task the_check_wav_task;
-static struct initial_delay_task the_initial_delay_task;
-
-static struct writer_node_group *wng;
-
-/** Length of a standard wav header. */
-#define WAV_HEADER_LEN 44
-
-/**
- * Test if audio buffer contains a valid wave header.
- *
- * \return If not, return -E_NO_WAV_HEADER, otherwise, return zero. If
- * there is less than WAV_HEADER_LEN bytes available, return one.
- */
-static void check_wav_pre_select(__a_unused struct sched *s, struct task *t)
-{
- struct check_wav_task *cwt = container_of(t, struct check_wav_task, task);
- unsigned char *a;
- int ret;
-
- if (*cwt->loaded < WAV_HEADER_LEN) {
- if (*cwt->input_error < 0)
- t->error = *cwt->input_error;
- return;
- }
- cwt->channels = 2;
- cwt->samplerate = 44100;
- a = (unsigned char*)cwt->buf;
- if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F') {
- PARA_NOTICE_LOG("wav header not found\n");
- t->error = -E_NO_WAV_HEADER;
- goto out;
- }
- cwt->channels = (unsigned) a[22];
- cwt->samplerate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
- *cwt->loaded -= WAV_HEADER_LEN;
- memmove(cwt->buf, cwt->buf + WAV_HEADER_LEN, *cwt->loaded);
- t->error = -E_WAV_HEADER_SUCCESS;
- PARA_INFO_LOG("channels: %d, sample rate: %d\n", cwt->channels, cwt->samplerate);
-out:
- wng->channels = &cwt->channels;
- wng->samplerate = &cwt->samplerate;
- ret = wng_open(wng);
- if (ret < 0)
- t->error = ret;
- s->timeout.tv_sec = 0;
- s->timeout.tv_usec = 1;
-}
-
-static void check_wav_pre_select_btr(__a_unused struct sched *s, struct task *t)
-{
- struct check_wav_task_btr *cwt = container_of(t, struct check_wav_task_btr, task);
-
- if (btr_get_input_queue_size(cwt->btrn) < WAV_HEADER_LEN)
- return;
- s->timeout.tv_sec = 0;
- s->timeout.tv_usec = 1;
-}
-
-static int check_wav_exec(struct btr_node *btrn, const char *cmd, char **result)
-{
- struct check_wav_task_btr *cwt = btr_context(btrn);
-
-
- if (!strcmp(cmd, "samplerate")) {
- if (cwt->state != CWS_HAVE_HEADER)
- return -ERRNO_TO_PARA_ERROR(ENAVAIL);
- *result = make_message("%d", cwt->samplerate);
- return 1;
- }
- if (!strcmp(cmd, "channels")) {
- if (cwt->state != CWS_HAVE_HEADER)
- return -ERRNO_TO_PARA_ERROR(ENAVAIL);
- *result = make_message("%d", cwt->channels);
- return 1;
- }
- return -ERRNO_TO_PARA_ERROR(ENOTSUP);
-}
-
-static void check_wav_post_select_btr(__a_unused struct sched *s, struct task *t)
-{
- struct check_wav_task_btr *cwt = container_of(t, struct check_wav_task_btr, task);
- unsigned char *a;
- size_t sz = btr_get_input_queue_size(cwt->btrn);
-
- t->error = 0;
- if (cwt->state != CWS_NEED_HEADER)
- goto out;
- if (sz < WAV_HEADER_LEN) {
- if (!btr_no_parent(cwt->btrn))
- return;
- if (sz != 0) {
- cwt->state = CWS_NO_HEADER;
- goto out;
- }
- t->error = -E_WRITE_EOF;
- goto err;
- }
- cwt->channels = 2;
- cwt->samplerate = 44100;
- btr_next_buffer(cwt->btrn, (char **)&a);
- if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' || a[3] != 'F') {
- PARA_NOTICE_LOG("wav header not found\n");
- cwt->state = CWS_NO_HEADER;
- sprintf(t->status, "check wav: no header");
- goto out;
- }
- PARA_INFO_LOG("found wav header\n");
- cwt->state = CWS_HAVE_HEADER;
- sprintf(t->status, "check wav: have header");
- cwt->channels = (unsigned) a[22];
- cwt->samplerate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
- PARA_INFO_LOG("channels: %d, sample rate: %d\n", cwt->channels, cwt->samplerate);
- btr_consume(cwt->btrn, WAV_HEADER_LEN);
-out:
- if (sz)
- btr_pushdown(cwt->btrn);
- else {
- if (btr_no_parent(cwt->btrn))
- t->error = -E_WRITE_EOF;
- }
-err:
- if (t->error < 0)
- btr_remove_node(cwt->btrn);
-}
-
-static void initial_delay_pre_select(struct sched *s, struct task *t)
-{
- struct initial_delay_task *idt = container_of(t, struct initial_delay_task, task);
- struct timeval diff;
-
- if (!idt->start_time.tv_sec && !idt->start_time.tv_usec) {
- t->error = -E_NO_DELAY;
- goto register_check_wav;
- }
- if (tv_diff(now, &idt->start_time, &diff) > 0) {
- t->error = -E_DELAY_TIMEOUT;
- goto register_check_wav;
- }
- if (tv_diff(&s->timeout , &diff, NULL) > 0)
- s->timeout = diff;
- return;
-register_check_wav:
- register_task(&the_check_wav_task.task);
- s->timeout.tv_sec = 0;
- s->timeout.tv_usec = 1;
-}
-