]> code.delx.au - pulseaudio/blob - src/pulsecore/sound-file-stream.c
never hand out more data from a sink input than requested. Otherwise the resampler...
[pulseaudio] / src / pulsecore / sound-file-stream.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2008 Lennart Poettering
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <errno.h>
34
35 #include <sndfile.h>
36
37 #include <pulse/xmalloc.h>
38 #include <pulse/util.h>
39
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/sink-input.h>
42 #include <pulsecore/log.h>
43 #include <pulsecore/thread-mq.h>
44 #include <pulsecore/core-util.h>
45 #include <pulsecore/sample-util.h>
46
47 #include "sound-file-stream.h"
48
49 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
50
51 typedef struct file_stream {
52 pa_msgobject parent;
53 pa_core *core;
54 pa_sink_input *sink_input;
55
56 SNDFILE *sndfile;
57 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames);
58
59 /* We need this memblockq here to easily fulfill rewind requests
60 * (even beyond the file start!) */
61 pa_memblockq *memblockq;
62 } file_stream;
63
64 enum {
65 FILE_STREAM_MESSAGE_UNLINK
66 };
67
68 PA_DECLARE_CLASS(file_stream);
69 #define FILE_STREAM(o) (file_stream_cast(o))
70 static PA_DEFINE_CHECK_TYPE(file_stream, pa_msgobject);
71
72 /* Called from main context */
73 static void file_stream_unlink(file_stream *u) {
74 pa_assert(u);
75
76 if (!u->sink_input)
77 return;
78
79 pa_sink_input_unlink(u->sink_input);
80 pa_sink_input_unref(u->sink_input);
81 u->sink_input = NULL;
82
83 /* Make sure we don't decrease the ref count twice. */
84 file_stream_unref(u);
85 }
86
87 /* Called from main context */
88 static void file_stream_free(pa_object *o) {
89 file_stream *u = FILE_STREAM(o);
90 pa_assert(u);
91
92 if (u->memblockq)
93 pa_memblockq_free(u->memblockq);
94
95 if (u->sndfile)
96 sf_close(u->sndfile);
97
98 pa_xfree(u);
99 }
100
101 /* Called from main context */
102 static int file_stream_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
103 file_stream *u = FILE_STREAM(o);
104 file_stream_assert_ref(u);
105
106 switch (code) {
107 case FILE_STREAM_MESSAGE_UNLINK:
108 file_stream_unlink(u);
109 break;
110 }
111
112 return 0;
113 }
114
115 /* Called from main context */
116 static void sink_input_kill_cb(pa_sink_input *i) {
117 file_stream *u;
118
119 pa_sink_input_assert_ref(i);
120 u = FILE_STREAM(i->userdata);
121 file_stream_assert_ref(u);
122
123 file_stream_unlink(u);
124 }
125
126 /* Called from IO thread context */
127 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
128 file_stream *u;
129
130 pa_sink_input_assert_ref(i);
131 u = FILE_STREAM(i->userdata);
132 file_stream_assert_ref(u);
133
134 /* If we are added for the first time, ask for a rewinding so that
135 * we are heard right-away. */
136 if (PA_SINK_INPUT_IS_LINKED(state) &&
137 i->thread_info.state == PA_SINK_INPUT_INIT)
138 pa_sink_input_request_rewind(i, 0, FALSE, TRUE);
139 }
140
141 /* Called from IO thread context */
142 static int sink_input_pop_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
143 file_stream *u;
144
145 pa_sink_input_assert_ref(i);
146 pa_assert(chunk);
147 u = FILE_STREAM(i->userdata);
148 file_stream_assert_ref(u);
149
150 if (!u->memblockq)
151 return -1;
152
153 for (;;) {
154 pa_memchunk tchunk;
155 size_t fs;
156 void *p;
157 sf_count_t n;
158
159 if (pa_memblockq_peek(u->memblockq, chunk) >= 0) {
160 chunk->length = PA_MIN(chunk->length, length);
161 pa_memblockq_drop(u->memblockq, chunk->length);
162 return 0;
163 }
164
165 if (!u->sndfile)
166 break;
167
168 tchunk.memblock = pa_memblock_new(i->sink->core->mempool, length);
169 tchunk.index = 0;
170
171 p = pa_memblock_acquire(tchunk.memblock);
172
173 if (u->readf_function) {
174 fs = pa_frame_size(&i->sample_spec);
175 n = u->readf_function(u->sndfile, p, length/fs);
176 } else {
177 fs = 1;
178 n = sf_read_raw(u->sndfile, p, length);
179 }
180
181 pa_memblock_release(tchunk.memblock);
182
183 if (n <= 0) {
184 pa_memblock_unref(tchunk.memblock);
185
186 sf_close(u->sndfile);
187 u->sndfile = NULL;
188 break;
189 }
190
191 tchunk.length = n * fs;
192
193 pa_memblockq_push(u->memblockq, &tchunk);
194 pa_memblock_unref(tchunk.memblock);
195 }
196
197 if (pa_sink_input_safe_to_remove(i)) {
198 pa_memblockq_free(u->memblockq);
199 u->memblockq = NULL;
200
201 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u), FILE_STREAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL);
202 }
203
204 return -1;
205 }
206
207 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
208 file_stream *u;
209
210 pa_sink_input_assert_ref(i);
211 pa_assert(nbytes > 0);
212 u = FILE_STREAM(i->userdata);
213 file_stream_assert_ref(u);
214
215 pa_log("backwards %lu", (unsigned long) nbytes);
216
217 if (!u->memblockq)
218 return;
219
220 pa_memblockq_rewind(u->memblockq, nbytes);
221 }
222
223 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
224 file_stream *u;
225
226 pa_sink_input_assert_ref(i);
227 u = FILE_STREAM(i->userdata);
228 file_stream_assert_ref(u);
229
230 if (!u->memblockq)
231 return;
232
233 pa_memblockq_set_maxrewind(u->memblockq, nbytes);
234 }
235
236 int pa_play_file(
237 pa_sink *sink,
238 const char *fname,
239 const pa_cvolume *volume) {
240
241 file_stream *u = NULL;
242 SF_INFO sfinfo;
243 pa_sample_spec ss;
244 pa_sink_input_new_data data;
245 int fd;
246
247 pa_assert(sink);
248 pa_assert(fname);
249
250 u = pa_msgobject_new(file_stream);
251 u->parent.parent.free = file_stream_free;
252 u->parent.process_msg = file_stream_process_msg;
253 u->core = sink->core;
254 u->sink_input = NULL;
255 u->sndfile = NULL;
256 u->readf_function = NULL;
257 u->memblockq = NULL;
258
259 memset(&sfinfo, 0, sizeof(sfinfo));
260
261 if ((fd = open(fname, O_RDONLY
262 #ifdef O_NOCTTY
263 |O_NOCTTY
264 #endif
265 )) < 0) {
266 pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
267 goto fail;
268 }
269
270 /* FIXME: For now we just use posix_fadvise to avoid page faults
271 * when accessing the file data. Eventually we should move the
272 * file reader into the main event loop and pass the data over the
273 * asyncmsgq. */
274
275 #ifdef HAVE_POSIX_FADVISE
276 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL) < 0) {
277 pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno));
278 goto fail;
279 } else
280 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
281
282 if (posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED) < 0) {
283 pa_log_warn("POSIX_FADV_WILLNEED failed: %s", pa_cstrerror(errno));
284 goto fail;
285 } else
286 pa_log_debug("POSIX_FADV_WILLNEED succeeded.");
287 #endif
288
289 if (!(u->sndfile = sf_open_fd(fd, SFM_READ, &sfinfo, 1))) {
290 pa_log("Failed to open file %s", fname);
291 pa_close(fd);
292 goto fail;
293 }
294
295 switch (sfinfo.format & 0xFF) {
296 case SF_FORMAT_PCM_16:
297 case SF_FORMAT_PCM_U8:
298 case SF_FORMAT_PCM_S8:
299 ss.format = PA_SAMPLE_S16NE;
300 u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
301 break;
302
303 case SF_FORMAT_ULAW:
304 ss.format = PA_SAMPLE_ULAW;
305 break;
306
307 case SF_FORMAT_ALAW:
308 ss.format = PA_SAMPLE_ALAW;
309 break;
310
311 case SF_FORMAT_FLOAT:
312 default:
313 ss.format = PA_SAMPLE_FLOAT32NE;
314 u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
315 break;
316 }
317
318 ss.rate = sfinfo.samplerate;
319 ss.channels = sfinfo.channels;
320
321 if (!pa_sample_spec_valid(&ss)) {
322 pa_log("Unsupported sample format in file %s", fname);
323 goto fail;
324 }
325
326 pa_sink_input_new_data_init(&data);
327 data.sink = sink;
328 data.driver = __FILE__;
329 pa_sink_input_new_data_set_sample_spec(&data, &ss);
330 pa_sink_input_new_data_set_volume(&data, volume);
331 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_NAME, pa_path_get_filename(fname));
332 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_FILENAME, fname);
333
334 u->sink_input = pa_sink_input_new(sink->core, &data, 0);
335 pa_sink_input_new_data_done(&data);
336
337 if (!u->sink_input)
338 goto fail;
339
340 u->sink_input->pop = sink_input_pop_cb;
341 u->sink_input->process_rewind = sink_input_process_rewind_cb;
342 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
343 u->sink_input->kill = sink_input_kill_cb;
344 u->sink_input->state_change = sink_input_state_change_cb;
345 u->sink_input->userdata = u;
346
347 u->memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, pa_frame_size(&ss), 1, 1, 0, NULL);
348
349 pa_sink_input_put(u->sink_input);
350
351 /* The reference to u is dangling here, because we want to keep
352 * this stream around until it is fully played. */
353
354 return 0;
355
356 fail:
357 if (u)
358 file_stream_unref(u);
359
360 return -1;
361 }