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>
45 #include "sound-file-stream.h"
47 typedef struct file_stream
{
51 pa_sink_input
*sink_input
;
53 sf_count_t (*readf_function
)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
);
58 FILE_STREAM_MESSAGE_UNLINK
61 PA_DECLARE_CLASS(file_stream
);
62 #define FILE_STREAM(o) (file_stream_cast(o))
63 static PA_DEFINE_CHECK_TYPE(file_stream
, pa_msgobject
);
65 static void file_stream_unlink(file_stream
*u
) {
71 pa_sink_input_unlink(u
->sink_input
);
73 pa_sink_input_unref(u
->sink_input
);
76 /* Make sure we don't decrease the ref count twice. */
80 static void file_stream_free(pa_object
*o
) {
81 file_stream
*u
= FILE_STREAM(o
);
84 file_stream_unlink(u
);
86 if (u
->memchunk
.memblock
)
87 pa_memblock_unref(u
->memchunk
.memblock
);
95 static int file_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
96 file_stream
*u
= FILE_STREAM(o
);
97 file_stream_assert_ref(u
);
100 case FILE_STREAM_MESSAGE_UNLINK
:
101 file_stream_unlink(u
);
108 static void sink_input_kill_cb(pa_sink_input
*i
) {
109 pa_sink_input_assert_ref(i
);
111 file_stream_unlink(FILE_STREAM(i
->userdata
));
114 static int sink_input_peek_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
) {
119 u
= FILE_STREAM(i
->userdata
);
120 file_stream_assert_ref(u
);
127 if (!u
->memchunk
.memblock
) {
129 u
->memchunk
.memblock
= pa_memblock_new(i
->sink
->core
->mempool
, length
);
130 u
->memchunk
.index
= 0;
132 if (u
->readf_function
) {
135 size_t fs
= pa_frame_size(&i
->sample_spec
);
137 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
138 n
= u
->readf_function(u
->sndfile
, p
, length
/fs
);
139 pa_memblock_release(u
->memchunk
.memblock
);
144 u
->memchunk
.length
= n
* fs
;
149 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
150 n
= sf_read_raw(u
->sndfile
, p
, length
);
151 pa_memblock_release(u
->memchunk
.memblock
);
156 u
->memchunk
.length
= n
;
159 if (u
->memchunk
.length
<= 0) {
161 pa_memblock_unref(u
->memchunk
.memblock
);
162 pa_memchunk_reset(&u
->memchunk
);
164 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(u
), FILE_STREAM_MESSAGE_UNLINK
, NULL
, 0, NULL
, NULL
);
166 sf_close(u
->sndfile
);
173 pa_assert(u
->memchunk
.memblock
);
174 pa_assert(u
->memchunk
.length
> 0);
176 if (u
->drop
< u
->memchunk
.length
) {
177 u
->memchunk
.index
+= u
->drop
;
178 u
->memchunk
.length
-= u
->drop
;
183 u
->drop
-= u
->memchunk
.length
;
184 pa_memblock_unref(u
->memchunk
.memblock
);
185 pa_memchunk_reset(&u
->memchunk
);
188 *chunk
= u
->memchunk
;
189 pa_memblock_ref(chunk
->memblock
);
191 pa_assert(chunk
->length
> 0);
192 pa_assert(u
->drop
<= 0);
197 static void sink_input_drop_cb(pa_sink_input
*i
, size_t length
) {
201 pa_assert(length
> 0);
202 u
= FILE_STREAM(i
->userdata
);
203 file_stream_assert_ref(u
);
205 if (u
->memchunk
.memblock
) {
207 if (length
< u
->memchunk
.length
) {
208 u
->memchunk
.index
+= length
;
209 u
->memchunk
.length
-= length
;
213 length
-= u
->memchunk
.length
;
214 pa_memblock_unref(u
->memchunk
.memblock
);
215 pa_memchunk_reset(&u
->memchunk
);
224 const pa_cvolume
*volume
) {
226 file_stream
*u
= NULL
;
229 pa_sink_input_new_data data
;
235 u
= pa_msgobject_new(file_stream
);
236 u
->parent
.parent
.free
= file_stream_free
;
237 u
->parent
.process_msg
= file_stream_process_msg
;
238 u
->core
= sink
->core
;
239 u
->sink_input
= NULL
;
240 pa_memchunk_reset(&u
->memchunk
);
242 u
->readf_function
= NULL
;
245 memset(&sfinfo
, 0, sizeof(sfinfo
));
247 if ((fd
= open(fname
, O_RDONLY
|O_NOCTTY
)) < 0) {
248 pa_log("Failed to open file %s: %s", fname
, pa_cstrerror(errno
));
252 /* FIXME: For now we just use posix_fadvise to avoid page faults
253 * when accessing the file data. Eventually we should move the
254 * file reader into the main event loop and pass the data over the
257 #ifdef HAVE_POSIX_FADVISE
258 if (posix_fadvise(fd
, 0, 0, POSIX_FADV_SEQUENTIAL
) < 0) {
259 pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno
));
262 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
264 if (posix_fadvise(fd
, 0, 0, POSIX_FADV_WILLNEED
) < 0) {
265 pa_log_warn("POSIX_FADV_WILLNEED failed: %s", pa_cstrerror(errno
));
268 pa_log_debug("POSIX_FADV_WILLNEED succeeded.");
271 if (!(u
->sndfile
= sf_open_fd(fd
, SFM_READ
, &sfinfo
, 1))) {
272 pa_log("Failed to open file %s", fname
);
277 switch (sfinfo
.format
& 0xFF) {
278 case SF_FORMAT_PCM_16
:
279 case SF_FORMAT_PCM_U8
:
280 case SF_FORMAT_PCM_S8
:
281 ss
.format
= PA_SAMPLE_S16NE
;
282 u
->readf_function
= (sf_count_t (*)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_short
;
286 ss
.format
= PA_SAMPLE_ULAW
;
290 ss
.format
= PA_SAMPLE_ALAW
;
293 case SF_FORMAT_FLOAT
:
295 ss
.format
= PA_SAMPLE_FLOAT32NE
;
296 u
->readf_function
= (sf_count_t (*)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_float
;
300 ss
.rate
= sfinfo
.samplerate
;
301 ss
.channels
= sfinfo
.channels
;
303 if (!pa_sample_spec_valid(&ss
)) {
304 pa_log("Unsupported sample format in file %s", fname
);
308 pa_sink_input_new_data_init(&data
);
310 data
.driver
= __FILE__
;
312 pa_sink_input_new_data_set_sample_spec(&data
, &ss
);
313 pa_sink_input_new_data_set_volume(&data
, volume
);
315 if (!(u
->sink_input
= pa_sink_input_new(sink
->core
, &data
, 0)))
318 u
->sink_input
->peek
= sink_input_peek_cb
;
319 u
->sink_input
->drop
= sink_input_drop_cb
;
320 u
->sink_input
->kill
= sink_input_kill_cb
;
321 u
->sink_input
->userdata
= u
;
323 pa_sink_input_put(u
->sink_input
);
325 /* The reference to u is dangling here, because we want to keep
326 * this stream around until it is fully played. */
332 file_stream_unref(u
);