4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
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.
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.
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
37 #include <pulse/xmalloc.h>
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>
46 #include "sound-file-stream.h"
48 #define MEMBLOCKQ_MAXLENGTH (16*1024*1024)
50 typedef struct file_stream
{
53 pa_sink_input
*sink_input
;
56 sf_count_t (*readf_function
)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
);
58 pa_memblockq
*memblockq
;
62 FILE_STREAM_MESSAGE_UNLINK
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
);
69 static void file_stream_unlink(file_stream
*u
) {
75 pa_sink_input_unlink(u
->sink_input
);
76 pa_sink_input_unref(u
->sink_input
);
79 /* Make sure we don't decrease the ref count twice. */
83 static void file_stream_free(pa_object
*o
) {
84 file_stream
*u
= FILE_STREAM(o
);
88 pa_memblockq_free(u
->memblockq
);
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
);
101 case FILE_STREAM_MESSAGE_UNLINK
:
102 file_stream_unlink(u
);
109 static void sink_input_kill_cb(pa_sink_input
*i
) {
112 pa_sink_input_assert_ref(i
);
113 u
= FILE_STREAM(i
->userdata
);
114 file_stream_assert_ref(u
);
116 file_stream_unlink(u
);
119 static int sink_input_pop_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
) {
122 pa_sink_input_assert_ref(i
);
124 u
= FILE_STREAM(i
->userdata
);
125 file_stream_assert_ref(u
);
130 pa_log_debug("pop: %lu", (unsigned long) length
);
135 if (pa_memblockq_peek(u
->memblockq
, chunk
) >= 0) {
136 pa_memblockq_drop(u
->memblockq
, chunk
->length
);
143 tchunk
.memblock
= pa_memblock_new(i
->sink
->core
->mempool
, length
);
146 if (u
->readf_function
) {
149 size_t fs
= pa_frame_size(&i
->sample_spec
);
151 p
= pa_memblock_acquire(tchunk
.memblock
);
152 n
= u
->readf_function(u
->sndfile
, p
, length
/fs
);
153 pa_memblock_release(tchunk
.memblock
);
158 tchunk
.length
= n
* fs
;
164 p
= pa_memblock_acquire(tchunk
.memblock
);
165 n
= sf_read_raw(u
->sndfile
, p
, length
);
166 pa_memblock_release(tchunk
.memblock
);
174 if (tchunk
.length
<= 0) {
176 pa_memblock_unref(tchunk
.memblock
);
178 sf_close(u
->sndfile
);
183 pa_memblockq_push(u
->memblockq
, &tchunk
);
184 pa_memblock_unref(tchunk
.memblock
);
187 pa_log_debug("peek fail");
189 if (pa_sink_input_safe_to_remove(i
)) {
190 pa_log_debug("completed to play");
192 pa_memblockq_free(u
->memblockq
);
195 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(u
), FILE_STREAM_MESSAGE_UNLINK
, NULL
, 0, NULL
, NULL
);
201 static void sink_input_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
204 pa_sink_input_assert_ref(i
);
205 pa_assert(nbytes
> 0);
206 u
= FILE_STREAM(i
->userdata
);
207 file_stream_assert_ref(u
);
212 pa_memblockq_rewind(u
->memblockq
, nbytes
);
215 static void sink_input_set_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
218 pa_sink_input_assert_ref(i
);
219 u
= FILE_STREAM(i
->userdata
);
220 file_stream_assert_ref(u
);
225 pa_memblockq_set_maxrewind(u
->memblockq
, nbytes
);
231 const pa_cvolume
*volume
) {
233 file_stream
*u
= NULL
;
236 pa_sink_input_new_data data
;
238 pa_memblock
*silence
;
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
;
249 u
->readf_function
= NULL
;
252 memset(&sfinfo
, 0, sizeof(sfinfo
));
254 if ((fd
= open(fname
, O_RDONLY
259 pa_log("Failed to open file %s: %s", fname
, pa_cstrerror(errno
));
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
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
));
273 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
275 if (posix_fadvise(fd
, 0, 0, POSIX_FADV_WILLNEED
) < 0) {
276 pa_log_warn("POSIX_FADV_WILLNEED failed: %s", pa_cstrerror(errno
));
279 pa_log_debug("POSIX_FADV_WILLNEED succeeded.");
282 if (!(u
->sndfile
= sf_open_fd(fd
, SFM_READ
, &sfinfo
, 1))) {
283 pa_log("Failed to open file %s", fname
);
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
;
297 ss
.format
= PA_SAMPLE_ULAW
;
301 ss
.format
= PA_SAMPLE_ALAW
;
304 case SF_FORMAT_FLOAT
:
306 ss
.format
= PA_SAMPLE_FLOAT32NE
;
307 u
->readf_function
= (sf_count_t (*)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_float
;
311 ss
.rate
= sfinfo
.samplerate
;
312 ss
.channels
= sfinfo
.channels
;
314 if (!pa_sample_spec_valid(&ss
)) {
315 pa_log("Unsupported sample format in file %s", fname
);
319 pa_sink_input_new_data_init(&data
);
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
);
327 u
->sink_input
= pa_sink_input_new(sink
->core
, &data
, 0);
328 pa_sink_input_new_data_done(&data
);
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
;
339 silence
= pa_silence_memblock_new(
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);
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
);
347 pa_sink_input_put(u
->sink_input
);
349 /* The reference to u is dangling here, because we want to keep
350 * this stream around until it is fully played. */
356 file_stream_unref(u
);