]> code.delx.au - pulseaudio/blob - src/pulsecore/sound-file-stream.c
add _cb suffix to _max_rewind function like with all other functions, too
[pulseaudio] / src / pulsecore / sound-file-stream.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2006 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 pa_memblockq *memblockq;
59 } file_stream;
60
61 enum {
62 FILE_STREAM_MESSAGE_UNLINK
63 };
64
65 PA_DECLARE_CLASS(file_stream);
66 #define FILE_STREAM(o) (file_stream_cast(o))
67 static PA_DEFINE_CHECK_TYPE(file_stream, pa_msgobject);
68
69 static void file_stream_unlink(file_stream *u) {
70 pa_assert(u);
71
72 if (!u->sink_input)
73 return;
74
75 pa_sink_input_unlink(u->sink_input);
76 pa_sink_input_unref(u->sink_input);
77 u->sink_input = NULL;
78
79 /* Make sure we don't decrease the ref count twice. */
80 file_stream_unref(u);
81 }
82
83 static void file_stream_free(pa_object *o) {
84 file_stream *u = FILE_STREAM(o);
85 pa_assert(u);
86
87 if (u->memblockq)
88 pa_memblockq_free(u->memblockq);
89
90 if (u->sndfile)
91 sf_close(u->sndfile);
92
93 pa_xfree(u);
94 }
95
96 static int file_stream_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
97 file_stream *u = FILE_STREAM(o);
98 file_stream_assert_ref(u);
99
100 switch (code) {
101 case FILE_STREAM_MESSAGE_UNLINK:
102 file_stream_unlink(u);
103 break;
104 }
105
106 return 0;
107 }
108
109 static void sink_input_kill_cb(pa_sink_input *i) {
110 file_stream *u;
111
112 pa_sink_input_assert_ref(i);
113 u = FILE_STREAM(i->userdata);
114 file_stream_assert_ref(u);
115
116 file_stream_unlink(u);
117 }
118
119 static int sink_input_pop_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
120 file_stream *u;
121
122 pa_sink_input_assert_ref(i);
123 pa_assert(chunk);
124 u = FILE_STREAM(i->userdata);
125 file_stream_assert_ref(u);
126
127 if (!u->memblockq)
128 return -1;
129
130 pa_log_debug("pop: %lu", (unsigned long) length);
131
132 for (;;) {
133 pa_memchunk tchunk;
134
135 if (pa_memblockq_peek(u->memblockq, chunk) >= 0) {
136 pa_memblockq_drop(u->memblockq, chunk->length);
137 return 0;
138 }
139
140 if (!u->sndfile)
141 break;
142
143 tchunk.memblock = pa_memblock_new(i->sink->core->mempool, length);
144 tchunk.index = 0;
145
146 if (u->readf_function) {
147 sf_count_t n;
148 void *p;
149 size_t fs = pa_frame_size(&i->sample_spec);
150
151 p = pa_memblock_acquire(tchunk.memblock);
152 n = u->readf_function(u->sndfile, p, length/fs);
153 pa_memblock_release(tchunk.memblock);
154
155 if (n <= 0)
156 n = 0;
157
158 tchunk.length = n * fs;
159
160 } else {
161 sf_count_t n;
162 void *p;
163
164 p = pa_memblock_acquire(tchunk.memblock);
165 n = sf_read_raw(u->sndfile, p, length);
166 pa_memblock_release(tchunk.memblock);
167
168 if (n <= 0)
169 n = 0;
170
171 tchunk.length = n;
172 }
173
174 if (tchunk.length <= 0) {
175
176 pa_memblock_unref(tchunk.memblock);
177
178 sf_close(u->sndfile);
179 u->sndfile = NULL;
180 break;
181 }
182
183 pa_memblockq_push(u->memblockq, &tchunk);
184 pa_memblock_unref(tchunk.memblock);
185 }
186
187 pa_log_debug("peek fail");
188
189 if (pa_sink_input_safe_to_remove(i)) {
190 pa_log_debug("completed to play");
191
192 pa_memblockq_free(u->memblockq);
193 u->memblockq = NULL;
194
195 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u), FILE_STREAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL);
196 }
197
198 return -1;
199 }
200
201 static void sink_input_rewind_cb(pa_sink_input *i, size_t nbytes) {
202 file_stream *u;
203
204 pa_sink_input_assert_ref(i);
205 pa_assert(nbytes > 0);
206 u = FILE_STREAM(i->userdata);
207 file_stream_assert_ref(u);
208
209 if (!u->memblockq)
210 return;
211
212 pa_memblockq_rewind(u->memblockq, nbytes);
213 }
214
215 static void sink_input_set_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
216 file_stream *u;
217
218 pa_sink_input_assert_ref(i);
219 u = FILE_STREAM(i->userdata);
220 file_stream_assert_ref(u);
221
222 if (!u->memblockq)
223 return;
224
225 pa_memblockq_set_maxrewind(u->memblockq, nbytes);
226 }
227
228 int pa_play_file(
229 pa_sink *sink,
230 const char *fname,
231 const pa_cvolume *volume) {
232
233 file_stream *u = NULL;
234 SF_INFO sfinfo;
235 pa_sample_spec ss;
236 pa_sink_input_new_data data;
237 int fd;
238 pa_memblock *silence;
239
240 pa_assert(sink);
241 pa_assert(fname);
242
243 u = pa_msgobject_new(file_stream);
244 u->parent.parent.free = file_stream_free;
245 u->parent.process_msg = file_stream_process_msg;
246 u->core = sink->core;
247 u->sink_input = NULL;
248 u->sndfile = NULL;
249 u->readf_function = NULL;
250 u->memblockq = NULL;
251
252 memset(&sfinfo, 0, sizeof(sfinfo));
253
254 if ((fd = open(fname, O_RDONLY
255 #ifdef O_NOCTTY
256 |O_NOCTTY
257 #endif
258 )) < 0) {
259 pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
260 goto fail;
261 }
262
263 /* FIXME: For now we just use posix_fadvise to avoid page faults
264 * when accessing the file data. Eventually we should move the
265 * file reader into the main event loop and pass the data over the
266 * asyncmsgq. */
267
268 #ifdef HAVE_POSIX_FADVISE
269 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL) < 0) {
270 pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno));
271 goto fail;
272 } else
273 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
274
275 if (posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED) < 0) {
276 pa_log_warn("POSIX_FADV_WILLNEED failed: %s", pa_cstrerror(errno));
277 goto fail;
278 } else
279 pa_log_debug("POSIX_FADV_WILLNEED succeeded.");
280 #endif
281
282 if (!(u->sndfile = sf_open_fd(fd, SFM_READ, &sfinfo, 1))) {
283 pa_log("Failed to open file %s", fname);
284 pa_close(fd);
285 goto fail;
286 }
287
288 switch (sfinfo.format & 0xFF) {
289 case SF_FORMAT_PCM_16:
290 case SF_FORMAT_PCM_U8:
291 case SF_FORMAT_PCM_S8:
292 ss.format = PA_SAMPLE_S16NE;
293 u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_short;
294 break;
295
296 case SF_FORMAT_ULAW:
297 ss.format = PA_SAMPLE_ULAW;
298 break;
299
300 case SF_FORMAT_ALAW:
301 ss.format = PA_SAMPLE_ALAW;
302 break;
303
304 case SF_FORMAT_FLOAT:
305 default:
306 ss.format = PA_SAMPLE_FLOAT32NE;
307 u->readf_function = (sf_count_t (*)(SNDFILE *sndfile, void *ptr, sf_count_t frames)) sf_readf_float;
308 break;
309 }
310
311 ss.rate = sfinfo.samplerate;
312 ss.channels = sfinfo.channels;
313
314 if (!pa_sample_spec_valid(&ss)) {
315 pa_log("Unsupported sample format in file %s", fname);
316 goto fail;
317 }
318
319 pa_sink_input_new_data_init(&data);
320 data.sink = sink;
321 data.driver = __FILE__;
322 pa_sink_input_new_data_set_sample_spec(&data, &ss);
323 pa_sink_input_new_data_set_volume(&data, volume);
324 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_NAME, fname);
325 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_FILENAME, fname);
326
327 u->sink_input = pa_sink_input_new(sink->core, &data, 0);
328 pa_sink_input_new_data_done(&data);
329
330 if (!u->sink_input)
331 goto fail;
332
333 u->sink_input->pop = sink_input_pop_cb;
334 u->sink_input->rewind = sink_input_rewind_cb;
335 u->sink_input->set_max_rewind = sink_input_set_max_rewind_cb;
336 u->sink_input->kill = sink_input_kill_cb;
337 u->sink_input->userdata = u;
338
339 silence = pa_silence_memblock_new(
340 u->core->mempool,
341 &u->sink_input->sample_spec,
342 u->sink_input->thread_info.resampler ? pa_resampler_max_block_size(u->sink_input->thread_info.resampler) : 0);
343
344 u->memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, pa_frame_size(&u->sink_input->sample_spec), 1, 1, 0, silence);
345 pa_memblock_unref(silence);
346
347 pa_sink_input_put(u->sink_input);
348
349 /* The reference to u is dangling here, because we want to keep
350 * this stream around until it is fully played. */
351
352 return 0;
353
354 fail:
355 if (u)
356 file_stream_unref(u);
357
358 return -1;
359 }