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>
44 #include "sound-file-stream.h"
46 #define BUF_SIZE (1024*16)
48 typedef struct file_stream
{
52 pa_sink_input
*sink_input
;
54 sf_count_t (*readf_function
)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
);
59 FILE_STREAM_MESSAGE_UNLINK
62 PA_DECLARE_CLASS(file_stream
);
63 #define FILE_STREAM(o) (file_stream_cast(o))
64 static PA_DEFINE_CHECK_TYPE(file_stream
, pa_msgobject
);
66 static void file_stream_unlink(file_stream
*u
) {
72 pa_sink_input_unlink(u
->sink_input
);
74 pa_sink_input_unref(u
->sink_input
);
77 /* Make sure we don't decrease the ref count twice. */
81 static void file_stream_free(pa_object
*o
) {
82 file_stream
*u
= FILE_STREAM(o
);
85 file_stream_unlink(u
);
87 if (u
->memchunk
.memblock
)
88 pa_memblock_unref(u
->memchunk
.memblock
);
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
) {
110 pa_sink_input_assert_ref(i
);
112 file_stream_unlink(FILE_STREAM(i
->userdata
));
115 static int sink_input_peek_cb(pa_sink_input
*i
, pa_memchunk
*chunk
) {
120 u
= FILE_STREAM(i
->userdata
);
121 file_stream_assert_ref(u
);
128 if (!u
->memchunk
.memblock
) {
130 u
->memchunk
.memblock
= pa_memblock_new(i
->sink
->core
->mempool
, BUF_SIZE
);
131 u
->memchunk
.index
= 0;
133 if (u
->readf_function
) {
136 size_t fs
= pa_frame_size(&i
->sample_spec
);
138 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
139 n
= u
->readf_function(u
->sndfile
, p
, BUF_SIZE
/fs
);
140 pa_memblock_release(u
->memchunk
.memblock
);
145 u
->memchunk
.length
= n
* fs
;
150 p
= pa_memblock_acquire(u
->memchunk
.memblock
);
151 n
= sf_read_raw(u
->sndfile
, p
, BUF_SIZE
);
152 pa_memblock_release(u
->memchunk
.memblock
);
157 u
->memchunk
.length
= n
;
160 if (u
->memchunk
.length
<= 0) {
162 pa_memblock_unref(u
->memchunk
.memblock
);
163 pa_memchunk_reset(&u
->memchunk
);
165 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(u
), FILE_STREAM_MESSAGE_UNLINK
, NULL
, 0, NULL
, NULL
);
167 sf_close(u
->sndfile
);
174 pa_assert(u
->memchunk
.memblock
);
175 pa_assert(u
->memchunk
.length
> 0);
177 if (u
->drop
< u
->memchunk
.length
) {
178 u
->memchunk
.index
+= u
->drop
;
179 u
->memchunk
.length
-= u
->drop
;
184 u
->drop
-= u
->memchunk
.length
;
185 pa_memblock_unref(u
->memchunk
.memblock
);
186 pa_memchunk_reset(&u
->memchunk
);
189 *chunk
= u
->memchunk
;
190 pa_memblock_ref(chunk
->memblock
);
192 pa_assert(chunk
->length
> 0);
193 pa_assert(u
->drop
<= 0);
198 static void sink_input_drop_cb(pa_sink_input
*i
, size_t length
) {
202 pa_assert(length
> 0);
203 u
= FILE_STREAM(i
->userdata
);
204 file_stream_assert_ref(u
);
206 if (u
->memchunk
.memblock
) {
208 if (length
< u
->memchunk
.length
) {
209 u
->memchunk
.index
+= length
;
210 u
->memchunk
.length
-= length
;
214 length
-= u
->memchunk
.length
;
215 pa_memblock_unref(u
->memchunk
.memblock
);
216 pa_memchunk_reset(&u
->memchunk
);
225 const pa_cvolume
*volume
) {
227 file_stream
*u
= NULL
;
230 pa_sink_input_new_data data
;
236 u
= pa_msgobject_new(file_stream
);
237 u
->parent
.parent
.free
= file_stream_free
;
238 u
->parent
.process_msg
= file_stream_process_msg
;
239 u
->core
= sink
->core
;
240 u
->sink_input
= NULL
;
241 pa_memchunk_reset(&u
->memchunk
);
243 u
->readf_function
= NULL
;
246 memset(&sfinfo
, 0, sizeof(sfinfo
));
248 if ((fd
= open(fname
, O_RDONLY
|O_NOCTTY
)) < 0) {
249 pa_log("Failed to open file %s: %s", fname
, pa_cstrerror(errno
));
253 /* FIXME: For now we just use posix_fadvise to avoid page faults
254 * when accessing the file data. Eventually we should move the
255 * file reader into the main event loop and pass the data over the
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.");
270 if (!(u
->sndfile
= sf_open_fd(fd
, SFM_READ
, &sfinfo
, 1))) {
271 pa_log("Failed to open file %s", fname
);
276 switch (sfinfo
.format
& 0xFF) {
277 case SF_FORMAT_PCM_16
:
278 case SF_FORMAT_PCM_U8
:
279 case SF_FORMAT_PCM_S8
:
280 ss
.format
= PA_SAMPLE_S16NE
;
281 u
->readf_function
= (sf_count_t (*)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_short
;
285 ss
.format
= PA_SAMPLE_ULAW
;
289 ss
.format
= PA_SAMPLE_ALAW
;
292 case SF_FORMAT_FLOAT
:
294 ss
.format
= PA_SAMPLE_FLOAT32NE
;
295 u
->readf_function
= (sf_count_t (*)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_float
;
299 ss
.rate
= sfinfo
.samplerate
;
300 ss
.channels
= sfinfo
.channels
;
302 if (!pa_sample_spec_valid(&ss
)) {
303 pa_log("Unsupported sample format in file %s", fname
);
307 pa_sink_input_new_data_init(&data
);
309 data
.driver
= __FILE__
;
311 pa_sink_input_new_data_set_sample_spec(&data
, &ss
);
312 pa_sink_input_new_data_set_volume(&data
, volume
);
314 if (!(u
->sink_input
= pa_sink_input_new(sink
->core
, &data
, 0)))
317 u
->sink_input
->peek
= sink_input_peek_cb
;
318 u
->sink_input
->drop
= sink_input_drop_cb
;
319 u
->sink_input
->kill
= sink_input_kill_cb
;
320 u
->sink_input
->userdata
= u
;
322 pa_sink_input_put(u
->sink_input
);
324 /* The reference to u is dangling here, because we want to keep
325 * this stream around until it is fully played. */
331 file_stream_unref(u
);