]> code.delx.au - pulseaudio/blob - src/pulsecore/sound-file-stream.c
merge glitch-free branch back into trunk
[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
39 #include <pulsecore/core-error.h>
40 #include <pulsecore/sink-input.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/thread-mq.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/sample-util.h>
45
46 #include "sound-file-stream.h"
47
48 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
49
50 typedef struct file_stream {
51 pa_msgobject parent;
52 pa_core *core;
53 pa_sink_input *sink_input;
54
55 SNDFILE *sndfile;
56 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames);
57
58 /* We need this memblockq here to easily fulfill rewind requests
59 * (even beyond the file start!) */
60 pa_memblockq *memblockq;
61 } file_stream;
62
63 enum {
64 FILE_STREAM_MESSAGE_UNLINK
65 };
66
67 PA_DECLARE_CLASS(file_stream);
68 #define FILE_STREAM(o) (file_stream_cast(o))
69 static PA_DEFINE_CHECK_TYPE(file_stream, pa_msgobject);
70
71 /* Called from main context */
72 static void file_stream_unlink(file_stream *u) {
73 pa_assert(u);
74
75 if (!u->sink_input)
76 return;
77
78 pa_sink_input_unlink(u->sink_input);
79 pa_sink_input_unref(u->sink_input);
80 u->sink_input = NULL;
81
82 /* Make sure we don't decrease the ref count twice. */
83 file_stream_unref(u);
84 }
85
86 /* Called from main context */
87 static void file_stream_free(pa_object *o) {
88 file_stream *u = FILE_STREAM(o);
89 pa_assert(u);
90
91 if (u->memblockq)
92 pa_memblockq_free(u->memblockq);
93
94 if (u->sndfile)
95 sf_close(u->sndfile);
96
97 pa_xfree(u);
98 }
99
100 /* Called from main context */
101 static int file_stream_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
102 file_stream *u = FILE_STREAM(o);
103 file_stream_assert_ref(u);
104
105 switch (code) {
106 case FILE_STREAM_MESSAGE_UNLINK:
107 file_stream_unlink(u);
108 break;
109 }
110
111 return 0;
112 }
113
114 /* Called from main context */
115 static void sink_input_kill_cb(pa_sink_input *i) {
116 file_stream *u;
117
118 pa_sink_input_assert_ref(i);
119 u = FILE_STREAM(i->userdata);
120 file_stream_assert_ref(u);
121
122 file_stream_unlink(u);
123 }
124
125 /* Called from IO thread context */
126 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
127 file_stream *u;
128
129 pa_sink_input_assert_ref(i);
130 u = FILE_STREAM(i->userdata);
131 file_stream_assert_ref(u);
132
133 /* If we are added for the first time, ask for a rewinding so that
134 * we are heard right-away. */
135 if (PA_SINK_INPUT_IS_LINKED(state) &&
136 i->thread_info.state == PA_SINK_INPUT_INIT)
137 pa_sink_input_request_rewind(i, 0, FALSE, TRUE);
138 }
139
140 /* Called from IO thread context */
141 static int sink_input_pop_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
142 file_stream *u;
143
144 pa_sink_input_assert_ref(i);
145 pa_assert(chunk);
146 u = FILE_STREAM(i->userdata);
147 file_stream_assert_ref(u);
148
149 if (!u->memblockq)
150 return -1;
151
152 pa_log_debug("pop: %lu", (unsigned long) length);
153
154 for (;;) {
155 pa_memchunk tchunk;
156 size_t fs;
157 void *p;
158 sf_count_t n;
159
160 if (pa_memblockq_peek(u->memblockq, chunk) >= 0) {
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 pa_log_debug("peek fail");
198
199 if (pa_sink_input_safe_to_remove(i)) {
200 pa_log_debug("completed to play");
201
202 pa_memblockq_free(u->memblockq);
203 u->memblockq = NULL;
204
205 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u), FILE_STREAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL);
206 }
207
208 return -1;
209 }
210
211 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
212 file_stream *u;
213
214 pa_sink_input_assert_ref(i);
215 pa_assert(nbytes > 0);
216 u = FILE_STREAM(i->userdata);
217 file_stream_assert_ref(u);
218
219 if (!u->memblockq)
220 return;
221
222 pa_memblockq_rewind(u->memblockq, nbytes);
223 }
224
225 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
226 file_stream *u;
227
228 pa_sink_input_assert_ref(i);
229 u = FILE_STREAM(i->userdata);
230 file_stream_assert_ref(u);
231
232 if (!u->memblockq)
233 return;
234
235 pa_memblockq_set_maxrewind(u->memblockq, nbytes);
236 }
237
238 int pa_play_file(
239 pa_sink *sink,
240 const char *fname,
241 const pa_cvolume *volume) {
242
243 file_stream *u = NULL;
244 SF_INFO sfinfo;
245 pa_sample_spec ss;
246 pa_sink_input_new_data data;
247 int fd;
248
249 pa_assert(sink);
250 pa_assert(fname);
251
252 u = pa_msgobject_new(file_stream);
253 u->parent.parent.free = file_stream_free;
254 u->parent.process_msg = file_stream_process_msg;
255 u->core = sink->core;
256 u->sink_input = NULL;
257 u->sndfile = NULL;
258 u->readf_function = NULL;
259 u->memblockq = NULL;
260
261 memset(&sfinfo, 0, sizeof(sfinfo));
262
263 if ((fd = open(fname, O_RDONLY
264 #ifdef O_NOCTTY
265 |O_NOCTTY
266 #endif
267 )) < 0) {
268 pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
269 goto fail;
270 }
271
272 /* FIXME: For now we just use posix_fadvise to avoid page faults
273 * when accessing the file data. Eventually we should move the
274 * file reader into the main event loop and pass the data over the
275 * asyncmsgq. */
276
277 #ifdef HAVE_POSIX_FADVISE
278 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL) < 0) {
279 pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno));
280 goto fail;
281 } else
282 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
283
284 if (posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED) < 0) {
285 pa_log_warn("POSIX_FADV_WILLNEED failed: %s", pa_cstrerror(errno));
286 goto fail;
287 } else
288 pa_log_debug("POSIX_FADV_WILLNEED succeeded.");
289 #endif
290
291 if (!(u->sndfile = sf_open_fd(fd, SFM_READ, &sfinfo, 1))) {
292 pa_log("Failed to open file %s", fname);
293 pa_close(fd);
294 goto fail;
295 }
296
297 switch (sfinfo.format & 0xFF) {
298 case SF_FORMAT_PCM_16:
299 case SF_FORMAT_PCM_U8:
300 case SF_FORMAT_PCM_S8:
301 ss.format = PA_SAMPLE_S16NE;
302 u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
303 break;
304
305 case SF_FORMAT_ULAW:
306 ss.format = PA_SAMPLE_ULAW;
307 break;
308
309 case SF_FORMAT_ALAW:
310 ss.format = PA_SAMPLE_ALAW;
311 break;
312
313 case SF_FORMAT_FLOAT:
314 default:
315 ss.format = PA_SAMPLE_FLOAT32NE;
316 u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
317 break;
318 }
319
320 ss.rate = sfinfo.samplerate;
321 ss.channels = sfinfo.channels;
322
323 if (!pa_sample_spec_valid(&ss)) {
324 pa_log("Unsupported sample format in file %s", fname);
325 goto fail;
326 }
327
328 pa_sink_input_new_data_init(&data);
329 data.sink = sink;
330 data.driver = __FILE__;
331 pa_sink_input_new_data_set_sample_spec(&data, &ss);
332 pa_sink_input_new_data_set_volume(&data, volume);
333 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_NAME, fname);
334 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_FILENAME, fname);
335
336 u->sink_input = pa_sink_input_new(sink->core, &data, 0);
337 pa_sink_input_new_data_done(&data);
338
339 if (!u->sink_input)
340 goto fail;
341
342 u->sink_input->pop = sink_input_pop_cb;
343 u->sink_input->process_rewind = sink_input_process_rewind_cb;
344 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
345 u->sink_input->kill = sink_input_kill_cb;
346 u->sink_input->state_change = sink_input_state_change_cb;
347 u->sink_input->userdata = u;
348
349 u->memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, pa_frame_size(&ss), 1, 1, 0, NULL);
350
351 pa_sink_input_put(u->sink_input);
352
353 /* The reference to u is dangling here, because we want to keep
354 * this stream around until it is fully played. */
355
356 return 0;
357
358 fail:
359 if (u)
360 file_stream_unref(u);
361
362 return -1;
363 }