2 This file is part of PulseAudio.
4 Copyright 2004-2008 Lennart Poettering
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.
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.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
32 #include <pulse/xmalloc.h>
33 #include <pulse/util.h>
35 #include <pulsecore/core-error.h>
36 #include <pulsecore/sink-input.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/thread-mq.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/mix.h>
41 #include <pulsecore/sndfile-util.h>
43 #include "sound-file-stream.h"
45 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
47 typedef struct file_stream {
50 pa_sink_input *sink_input;
53 sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames);
58 /* We need this memblockq here to easily fulfill rewind requests
59 * (even beyond the file start!) */
60 pa_memblockq *memblockq;
64 FILE_STREAM_MESSAGE_UNLINK
67 PA_DEFINE_PRIVATE_CLASS(file_stream, pa_msgobject);
68 #define FILE_STREAM(o) (file_stream_cast(o))
70 /* Called from main context */
71 static void file_stream_unlink(file_stream *u) {
77 pa_sink_input_unlink(u->sink_input);
78 pa_sink_input_unref(u->sink_input);
81 /* Make sure we don't decrease the ref count twice. */
85 /* Called from main context */
86 static void file_stream_free(pa_object *o) {
87 file_stream *u = FILE_STREAM(o);
91 pa_memblockq_free(u->memblockq);
99 /* Called from main context */
100 static int file_stream_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
101 file_stream *u = FILE_STREAM(o);
102 file_stream_assert_ref(u);
105 case FILE_STREAM_MESSAGE_UNLINK:
106 file_stream_unlink(u);
113 /* Called from main context */
114 static void sink_input_kill_cb(pa_sink_input *i) {
117 pa_sink_input_assert_ref(i);
118 u = FILE_STREAM(i->userdata);
119 file_stream_assert_ref(u);
121 file_stream_unlink(u);
124 /* Called from IO thread context */
125 static void sink_input_state_change_cb(pa_sink_input *i, pa_sink_input_state_t state) {
128 pa_sink_input_assert_ref(i);
129 u = FILE_STREAM(i->userdata);
130 file_stream_assert_ref(u);
132 /* If we are added for the first time, ask for a rewinding so that
133 * we are heard right-away. */
134 if (PA_SINK_INPUT_IS_LINKED(state) &&
135 i->thread_info.state == PA_SINK_INPUT_INIT && i->sink)
136 pa_sink_input_request_rewind(i, 0, false, true, true);
139 /* Called from IO thread context */
140 static int sink_input_pop_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
143 pa_sink_input_assert_ref(i);
145 u = FILE_STREAM(i->userdata);
146 file_stream_assert_ref(u);
157 if (pa_memblockq_peek(u->memblockq, chunk) >= 0) {
158 chunk->length = PA_MIN(chunk->length, length);
159 pa_memblockq_drop(u->memblockq, chunk->length);
166 tchunk.memblock = pa_memblock_new(i->sink->core->mempool, length);
169 p = pa_memblock_acquire(tchunk.memblock);
171 if (u->readf_function) {
172 fs = pa_frame_size(&i->sample_spec);
173 n = u->readf_function(u->sndfile, p, (sf_count_t) (length/fs));
176 n = sf_read_raw(u->sndfile, p, (sf_count_t) length);
179 pa_memblock_release(tchunk.memblock);
183 if (u->repeat == -1 || --u->repeat > 0) {
184 sf_seek(u->sndfile, 0, SEEK_SET);
185 pa_memblock_unref(tchunk.memblock);
189 pa_memblock_unref(tchunk.memblock);
191 sf_close(u->sndfile);
196 tchunk.length = (size_t) n * fs;
199 /* check invalid sample size */
200 if (PA_UNLIKELY(tchunk.length % pa_frame_size(&i->sample_spec))) {
201 pa_log_warn("Dropped not aligned samples. length(%zu), fs(%zu)",
202 tchunk.length, pa_frame_size(&i->sample_spec));
203 tchunk.length = PA_ROUND_DOWN(n, pa_frame_size(&i->sample_spec));
204 if (tchunk.length == 0) {
205 pa_memblock_unref(tchunk.memblock);
211 pa_memblockq_push(u->memblockq, &tchunk);
212 pa_memblock_unref(tchunk.memblock);
215 if (pa_sink_input_safe_to_remove(i)) {
216 pa_memblockq_free(u->memblockq);
219 pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u), FILE_STREAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL);
225 static void sink_input_process_rewind_cb(pa_sink_input *i, size_t nbytes) {
228 pa_sink_input_assert_ref(i);
229 u = FILE_STREAM(i->userdata);
230 file_stream_assert_ref(u);
235 pa_memblockq_rewind(u->memblockq, nbytes);
238 static void sink_input_update_max_rewind_cb(pa_sink_input *i, size_t nbytes) {
241 pa_sink_input_assert_ref(i);
242 u = FILE_STREAM(i->userdata);
243 file_stream_assert_ref(u);
248 pa_memblockq_set_maxrewind(u->memblockq, nbytes);
254 const pa_cvolume *volume) {
256 file_stream *u = NULL;
259 pa_sink_input_new_data data;
267 u = pa_msgobject_new(file_stream);
268 u->parent.parent.free = file_stream_free;
269 u->parent.process_msg = file_stream_process_msg;
270 u->core = sink->core;
271 u->sink_input = NULL;
273 u->readf_function = NULL;
276 if ((fd = pa_open_cloexec(fname, O_RDONLY, 0)) < 0) {
277 pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
281 /* FIXME: For now we just use posix_fadvise to avoid page faults
282 * when accessing the file data. Eventually we should move the
283 * file reader into the main event loop and pass the data over the
286 #ifdef HAVE_POSIX_FADVISE
287 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL) < 0) {
288 pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno));
291 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
293 if (posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED) < 0) {
294 pa_log_warn("POSIX_FADV_WILLNEED failed: %s", pa_cstrerror(errno));
297 pa_log_debug("POSIX_FADV_WILLNEED succeeded.");
301 if (!(u->sndfile = sf_open_fd(fd, SFM_READ, &sfi, 1))) {
302 pa_log("Failed to open file %s", fname);
311 if (pa_sndfile_read_sample_spec(u->sndfile, &ss) < 0) {
312 pa_log("Failed to determine file sample format.");
316 if (pa_sndfile_read_channel_map(u->sndfile, &cm) < 0) {
318 pa_log_info("Failed to determine file channel map, synthesizing one.");
319 pa_channel_map_init_extend(&cm, ss.channels, PA_CHANNEL_MAP_DEFAULT);
322 u->readf_function = pa_sndfile_readf_function(&ss);
324 pa_sink_input_new_data_init(&data);
325 pa_sink_input_new_data_set_sink(&data, sink, false, true);
326 data.driver = __FILE__;
327 pa_sink_input_new_data_set_sample_spec(&data, &ss);
328 pa_sink_input_new_data_set_channel_map(&data, &cm);
329 pa_sink_input_new_data_set_volume(&data, volume);
330 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_NAME, pa_path_get_filename(fname));
331 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_FILENAME, fname);
332 pa_sndfile_init_proplist(u->sndfile, data.proplist);
334 pa_sink_input_new(&u->sink_input, sink->core, &data);
335 pa_sink_input_new_data_done(&data);
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;
347 pa_sink_input_get_silence(u->sink_input, &silence);
348 u->memblockq = pa_memblockq_new("sound-file-stream memblockq", 0, MEMBLOCKQ_MAXLENGTH, 0, &ss, 1, 1, 0, &silence);
349 pa_memblock_unref(silence.memblock);
351 pa_sink_input_put(u->sink_input);
353 /* The reference to u is dangling here, because we want to keep
354 * this stream around until it is fully played. */
359 file_stream_unref(u);
368 int pa_play_file_repeat(
371 const pa_cvolume *volume,
375 uint32_t *stream_idx) {
377 file_stream *u = NULL;
380 pa_sink_input_new_data data;
387 u = pa_msgobject_new(file_stream);
388 u->parent.parent.free = file_stream_free;
389 u->parent.process_msg = file_stream_process_msg;
390 u->core = sink->core;
391 u->sink_input = NULL;
393 u->readf_function = NULL;
395 u->repeat = (repeat == 0) ? -1 : (int32_t)repeat;
398 if ((fd = pa_open_cloexec(fname, O_RDONLY, 0)) < 0) {
399 pa_log("Failed to open file %s: %s", fname, pa_cstrerror(errno));
404 /* FIXME: For now we just use posix_fadvise to avoid page faults
405 * when accessing the file data. Eventually we should move the
406 * file reader into the main event loop and pass the data over the
409 #ifdef HAVE_POSIX_FADVISE
410 if (posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL) < 0) {
411 pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno));
414 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
416 if (posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED) < 0) {
417 pa_log_warn("POSIX_FADV_WILLNEED failed: %s", pa_cstrerror(errno));
420 pa_log_debug("POSIX_FADV_WILLNEED succeeded.");
424 if (!(u->sndfile = sf_open_fd(fd, SFM_READ, &sfi, 1))) {
425 pa_log("Failed to open file %s", fname);
434 if (pa_sndfile_read_sample_spec(u->sndfile, &ss) < 0) {
435 pa_log("Failed to determine file sample format.");
439 if (pa_sndfile_read_channel_map(u->sndfile, &cm) < 0) {
441 pa_log_info("Failed to determine file channel map, synthesizing one.");
442 pa_channel_map_init_extend(&cm, ss.channels, PA_CHANNEL_MAP_DEFAULT);
445 u->readf_function = pa_sndfile_readf_function(&ss);
447 pa_sink_input_new_data_init(&data);
448 pa_sink_input_new_data_set_sink(&data, sink, false, true);
449 data.driver = __FILE__;
450 pa_sink_input_new_data_set_sample_spec(&data, &ss);
451 pa_sink_input_new_data_set_channel_map(&data, &cm);
452 pa_sink_input_new_data_set_volume(&data, volume);
453 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_NAME, pa_path_get_filename(fname));
454 pa_proplist_sets(data.proplist, PA_PROP_MEDIA_FILENAME, fname);
455 pa_proplist_update(data.proplist, PA_UPDATE_MERGE, p);
456 pa_sndfile_init_proplist(u->sndfile, data.proplist);
458 pa_sink_input_new(&u->sink_input, sink->core, &data);
459 pa_sink_input_new_data_done(&data);
464 u->sink_input->pop = sink_input_pop_cb;
465 u->sink_input->process_rewind = sink_input_process_rewind_cb;
466 u->sink_input->update_max_rewind = sink_input_update_max_rewind_cb;
467 u->sink_input->kill = sink_input_kill_cb;
468 u->sink_input->state_change = sink_input_state_change_cb;
469 u->sink_input->userdata = u;
471 pa_sink_input_get_silence(u->sink_input, &silence);
472 u->memblockq = pa_memblockq_new("sound-file-stream memblockq", 0, MEMBLOCKQ_MAXLENGTH, 0, &ss, 1, 1, 0, &silence);
473 pa_memblock_unref(silence.memblock);
475 pa_sink_input_put(u->sink_input);
476 *stream_idx = u->sink_input->index;
478 /* The reference to u is dangling here, because we want to keep
479 * this stream around until it is fully played. */
484 file_stream_unref(u);