Fix stream grabbing.
[paraslash.git] / wav_filter.c
1 /*
2  * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file wav_filter.c A filter that inserts a wave header. */
8
9 #include <regex.h>
10 #include <stdbool.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.h"
17 #include "buffer_tree.h"
18 #include "filter.h"
19 #include "string.h"
20 #include "portable_io.h"
21
22 /** size of the output buffer */
23 #define WAV_OUTBUF_SIZE 81920
24 /** a wav header is always 44 bytes */
25 #define WAV_HEADER_LEN 44
26 /** always write 16 bit header */
27 #define BITS 16
28
29 static void make_wav_header(unsigned int channels, unsigned int samplerate,
30                 char *headbuf)
31 {
32
33         unsigned int size = 0x7fffffff;
34         int bytespersec = channels * samplerate * BITS / 8;
35         int align = channels * BITS / 8;
36
37         PARA_DEBUG_LOG("writing wave header: %d channels, %d KHz\n", channels, samplerate);
38         memset(headbuf, 0, WAV_HEADER_LEN);
39         memcpy(headbuf, "RIFF", 4);
40         write_u32(headbuf + 4, size - 8);
41         memcpy(headbuf + 8, "WAVE", 4);
42         memcpy(headbuf + 12, "fmt ", 4);
43         write_u32(headbuf + 16, 16); /* 16 + extra format bytes (zero) */
44         write_u16(headbuf + 20, 1);     /* format (1 == PCM/uncompressed) */
45         write_u16(headbuf + 22, channels);
46         write_u32(headbuf + 24, samplerate);
47         write_u32(headbuf + 28, bytespersec);
48         write_u16(headbuf + 32, align); /* number of bytes per sample slice */
49         write_u16(headbuf + 34, BITS); /* significant bits per sample */
50         memcpy(headbuf + 36, "data", 4); /* chunk ID */
51         write_u32(headbuf + 40, size - 44); /* chunk size */
52 }
53
54 static ssize_t wav_convert(char *inbuf, size_t len, struct filter_node *fn)
55 {
56         size_t copy;
57         int *bof = fn->private_data;
58
59         if (*bof) {
60                 if (!len)
61                         return 0;
62                 if (!fn->fc->channels || !fn->fc->samplerate) {
63                         PARA_ERROR_LOG("%s\n", para_strerror(E_WAV_BAD_FC));
64                         return -E_WAV_BAD_FC;
65                 }
66                 make_wav_header(fn->fc->channels, fn->fc->samplerate, fn->buf);
67                 fn->loaded = WAV_HEADER_LEN;
68                 *bof = 0;
69 //              return 0;
70         }
71         copy = PARA_MIN(len, fn->bufsize - fn->loaded);
72         memmove(fn->buf + fn->loaded, inbuf, copy);
73         fn->loaded += copy;
74 //      PARA_DEBUG_LOG("len = %d, copy = %d\n", len, copy);
75         return copy;
76 }
77
78 static void wav_close(struct filter_node *fn)
79 {
80         free(fn->buf);
81         fn->buf = NULL;
82         free(fn->private_data);
83         fn->private_data = NULL;
84 }
85
86 static void wav_open(struct filter_node *fn)
87 {
88         int *bof;
89
90         fn->bufsize = WAV_OUTBUF_SIZE;
91         fn->buf = para_malloc(fn->bufsize);
92         fn->private_data = para_malloc(sizeof(int));
93         bof = fn->private_data;
94         fn->loaded = 0;
95         *bof = 1;
96         PARA_INFO_LOG("wav filter node: %p, output buffer: %p, loaded: %zd\n",
97                 fn, fn->buf, fn->loaded);
98 }
99
100 static void wav_pre_select(struct sched *s, struct task *t)
101 {
102         struct filter_node *fn = container_of(t, struct filter_node, task);
103         size_t iqs = btr_get_input_queue_size(fn->btrn);
104
105         t->error = 0;
106         if (iqs == 0)
107                 return;
108         s->timeout.tv_sec = 0;
109         s->timeout.tv_usec = 1;
110 }
111
112 static void wav_post_select(__a_unused struct sched *s, struct task *t)
113 {
114         struct filter_node *fn = container_of(t, struct filter_node, task);
115         struct btr_node *btrn = fn->btrn;
116         size_t iqs = btr_get_input_queue_size(btrn);
117         int ret;
118         char *header, *buf;
119         int32_t rate, ch;
120
121
122         t->error = 0;
123         if (iqs == 0) {
124                 ret = -E_WAV_EOF;
125                 if (btr_no_parent(btrn))
126                         goto err;
127                 return;
128         }
129         ret = btr_exec_up(btrn, "samplerate", &buf);
130         if (ret < 0) {
131                 ret = -E_WAV_BAD_FC;
132                 goto err;
133         }
134         ret = para_atoi32(buf, &rate);
135         free(buf);
136         if (ret < 0)
137                 goto err;
138         ret = btr_exec_up(btrn, "channels", &buf);
139         if (ret < 0) {
140                 ret = -E_WAV_BAD_FC;
141                 goto err;
142         }
143         ret = para_atoi32(buf, &ch);
144         free(buf);
145         if (ret < 0)
146                 goto err;
147         header = para_malloc(WAV_HEADER_LEN);
148         make_wav_header(ch, rate, header);
149         btr_add_output(header, WAV_HEADER_LEN, btrn);
150         ret = -E_WAV_SUCCESS;
151 err:
152         t->error = ret;
153         if (ret == -E_WAV_SUCCESS)
154                 btr_splice_out_node(btrn);
155         else {
156                 btr_remove_node(btrn);
157                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
158         }
159 }
160
161 /**
162  * the init function of the wav filter
163  *
164  * \param f struct to initialize
165  */
166 void wav_filter_init(struct filter *f)
167 {
168         f->convert = wav_convert;
169         f->close = wav_close;
170         f->open = wav_open;
171         f->pre_select = wav_pre_select;
172         f->post_select = wav_post_select;
173 }