4 This file is part of PulseAudio.
6 Copyright 2004-2008 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 /* 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_DECLARE_CLASS(file_stream
);
68 #define FILE_STREAM(o) (file_stream_cast(o))
69 static PA_DEFINE_CHECK_TYPE(file_stream
, pa_msgobject
);
71 /* Called from main context */
72 static void file_stream_unlink(file_stream
*u
) {
78 pa_sink_input_unlink(u
->sink_input
);
79 pa_sink_input_unref(u
->sink_input
);
82 /* Make sure we don't decrease the ref count twice. */
86 /* Called from main context */
87 static void file_stream_free(pa_object
*o
) {
88 file_stream
*u
= FILE_STREAM(o
);
92 pa_memblockq_free(u
->memblockq
);
100 /* Called from main context */
101 static int file_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
102 file_stream
*u
= FILE_STREAM(o
);
103 file_stream_assert_ref(u
);
106 case FILE_STREAM_MESSAGE_UNLINK
:
107 file_stream_unlink(u
);
114 /* Called from main context */
115 static void sink_input_kill_cb(pa_sink_input
*i
) {
118 pa_sink_input_assert_ref(i
);
119 u
= FILE_STREAM(i
->userdata
);
120 file_stream_assert_ref(u
);
122 file_stream_unlink(u
);
125 /* Called from IO thread context */
126 static void sink_input_state_change_cb(pa_sink_input
*i
, pa_sink_input_state_t state
) {
129 pa_sink_input_assert_ref(i
);
130 u
= FILE_STREAM(i
->userdata
);
131 file_stream_assert_ref(u
);
133 /* If we are added for the first time, ask for a rewinding so that
134 * we are heard right-away. */
135 if (PA_SINK_INPUT_IS_LINKED(state
) &&
136 i
->thread_info
.state
== PA_SINK_INPUT_INIT
)
137 pa_sink_input_request_rewind(i
, 0, FALSE
, TRUE
);
140 /* Called from IO thread context */
141 static int sink_input_pop_cb(pa_sink_input
*i
, size_t length
, pa_memchunk
*chunk
) {
144 pa_sink_input_assert_ref(i
);
146 u
= FILE_STREAM(i
->userdata
);
147 file_stream_assert_ref(u
);
152 pa_log_debug("pop: %lu", (unsigned long) length
);
160 if (pa_memblockq_peek(u
->memblockq
, chunk
) >= 0) {
161 pa_memblockq_drop(u
->memblockq
, chunk
->length
);
168 tchunk
.memblock
= pa_memblock_new(i
->sink
->core
->mempool
, length
);
171 p
= pa_memblock_acquire(tchunk
.memblock
);
173 if (u
->readf_function
) {
174 fs
= pa_frame_size(&i
->sample_spec
);
175 n
= u
->readf_function(u
->sndfile
, p
, length
/fs
);
178 n
= sf_read_raw(u
->sndfile
, p
, length
);
181 pa_memblock_release(tchunk
.memblock
);
184 pa_memblock_unref(tchunk
.memblock
);
186 sf_close(u
->sndfile
);
191 tchunk
.length
= n
* fs
;
193 pa_memblockq_push(u
->memblockq
, &tchunk
);
194 pa_memblock_unref(tchunk
.memblock
);
197 pa_log_debug("peek fail");
199 if (pa_sink_input_safe_to_remove(i
)) {
200 pa_log_debug("completed to play");
202 pa_memblockq_free(u
->memblockq
);
205 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(u
), FILE_STREAM_MESSAGE_UNLINK
, NULL
, 0, NULL
, NULL
);
211 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
214 pa_sink_input_assert_ref(i
);
215 pa_assert(nbytes
> 0);
216 u
= FILE_STREAM(i
->userdata
);
217 file_stream_assert_ref(u
);
222 pa_memblockq_rewind(u
->memblockq
, nbytes
);
225 static void sink_input_update_max_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_set_maxrewind(u
->memblockq
, nbytes
);
241 const pa_cvolume
*volume
) {
243 file_stream
*u
= NULL
;
246 pa_sink_input_new_data data
;
252 u
= pa_msgobject_new(file_stream
);
253 u
->parent
.parent
.free
= file_stream_free
;
254 u
->parent
.process_msg
= file_stream_process_msg
;
255 u
->core
= sink
->core
;
256 u
->sink_input
= NULL
;
258 u
->readf_function
= NULL
;
261 memset(&sfinfo
, 0, sizeof(sfinfo
));
263 if ((fd
= open(fname
, O_RDONLY
268 pa_log("Failed to open file %s: %s", fname
, pa_cstrerror(errno
));
272 /* FIXME: For now we just use posix_fadvise to avoid page faults
273 * when accessing the file data. Eventually we should move the
274 * file reader into the main event loop and pass the data over the
277 #ifdef HAVE_POSIX_FADVISE
278 if (posix_fadvise(fd
, 0, 0, POSIX_FADV_SEQUENTIAL
) < 0) {
279 pa_log_warn("POSIX_FADV_SEQUENTIAL failed: %s", pa_cstrerror(errno
));
282 pa_log_debug("POSIX_FADV_SEQUENTIAL succeeded.");
284 if (posix_fadvise(fd
, 0, 0, POSIX_FADV_WILLNEED
) < 0) {
285 pa_log_warn("POSIX_FADV_WILLNEED failed: %s", pa_cstrerror(errno
));
288 pa_log_debug("POSIX_FADV_WILLNEED succeeded.");
291 if (!(u
->sndfile
= sf_open_fd(fd
, SFM_READ
, &sfinfo
, 1))) {
292 pa_log("Failed to open file %s", fname
);
297 switch (sfinfo
.format
& 0xFF) {
298 case SF_FORMAT_PCM_16
:
299 case SF_FORMAT_PCM_U8
:
300 case SF_FORMAT_PCM_S8
:
301 ss
.format
= PA_SAMPLE_S16NE
;
302 u
->readf_function
= (sf_count_t (*)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_short
;
306 ss
.format
= PA_SAMPLE_ULAW
;
310 ss
.format
= PA_SAMPLE_ALAW
;
313 case SF_FORMAT_FLOAT
:
315 ss
.format
= PA_SAMPLE_FLOAT32NE
;
316 u
->readf_function
= (sf_count_t (*)(SNDFILE
*sndfile
, void *ptr
, sf_count_t frames
)) sf_readf_float
;
320 ss
.rate
= sfinfo
.samplerate
;
321 ss
.channels
= sfinfo
.channels
;
323 if (!pa_sample_spec_valid(&ss
)) {
324 pa_log("Unsupported sample format in file %s", fname
);
328 pa_sink_input_new_data_init(&data
);
330 data
.driver
= __FILE__
;
331 pa_sink_input_new_data_set_sample_spec(&data
, &ss
);
332 pa_sink_input_new_data_set_volume(&data
, volume
);
333 pa_proplist_sets(data
.proplist
, PA_PROP_MEDIA_NAME
, fname
);
334 pa_proplist_sets(data
.proplist
, PA_PROP_MEDIA_FILENAME
, fname
);
336 u
->sink_input
= pa_sink_input_new(sink
->core
, &data
, 0);
337 pa_sink_input_new_data_done(&data
);
342 u
->sink_input
->pop
= sink_input_pop_cb
;
343 u
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
344 u
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
345 u
->sink_input
->kill
= sink_input_kill_cb
;
346 u
->sink_input
->state_change
= sink_input_state_change_cb
;
347 u
->sink_input
->userdata
= u
;
349 u
->memblockq
= pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH
, 0, pa_frame_size(&ss
), 1, 1, 0, NULL
);
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. */
360 file_stream_unref(u
);