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