diff options
Diffstat (limited to 'src/pulsecore/sound-file-stream.c')
-rw-r--r-- | src/pulsecore/sound-file-stream.c | 179 |
1 files changed, 99 insertions, 80 deletions
diff --git a/src/pulsecore/sound-file-stream.c b/src/pulsecore/sound-file-stream.c index bb1f3e9a..60c2560e 100644 --- a/src/pulsecore/sound-file-stream.c +++ b/src/pulsecore/sound-file-stream.c @@ -41,17 +41,21 @@ #include <pulsecore/log.h> #include <pulsecore/thread-mq.h> #include <pulsecore/core-util.h> +#include <pulsecore/sample-util.h> #include "sound-file-stream.h" +#define MEMBLOCKQ_MAXLENGTH (16*1024*1024) + typedef struct file_stream { pa_msgobject parent; pa_core *core; - SNDFILE *sndfile; pa_sink_input *sink_input; - pa_memchunk memchunk; + + SNDFILE *sndfile; sf_count_t (*readf_function)(SNDFILE *sndfile, void *ptr, sf_count_t frames); - size_t drop; + + pa_memblockq *memblockq; } file_stream; enum { @@ -69,7 +73,6 @@ static void file_stream_unlink(file_stream *u) { return; pa_sink_input_unlink(u->sink_input); - pa_sink_input_unref(u->sink_input); u->sink_input = NULL; @@ -81,10 +84,8 @@ static void file_stream_free(pa_object *o) { file_stream *u = FILE_STREAM(o); pa_assert(u); - file_stream_unlink(u); - - if (u->memchunk.memblock) - pa_memblock_unref(u->memchunk.memblock); + if (u->memblockq) + pa_memblockq_free(u->memblockq); if (u->sndfile) sf_close(u->sndfile); @@ -106,116 +107,122 @@ static int file_stream_process_msg(pa_msgobject *o, int code, void*userdata, int } static void sink_input_kill_cb(pa_sink_input *i) { + file_stream *u; + pa_sink_input_assert_ref(i); + u = FILE_STREAM(i->userdata); + file_stream_assert_ref(u); - file_stream_unlink(FILE_STREAM(i->userdata)); + file_stream_unlink(u); } -static int sink_input_peek_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) { +static int sink_input_pop_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) { file_stream *u; - pa_assert(i); + pa_sink_input_assert_ref(i); pa_assert(chunk); u = FILE_STREAM(i->userdata); file_stream_assert_ref(u); - if (!u->sndfile) + if (!u->memblockq) return -1; - for (;;) { - - if (!u->memchunk.memblock) { + pa_log_debug("pop: %lu", (unsigned long) length); - u->memchunk.memblock = pa_memblock_new(i->sink->core->mempool, length); - u->memchunk.index = 0; - - if (u->readf_function) { - sf_count_t n; - void *p; - size_t fs = pa_frame_size(&i->sample_spec); + for (;;) { + pa_memchunk tchunk; - p = pa_memblock_acquire(u->memchunk.memblock); - n = u->readf_function(u->sndfile, p, length/fs); - pa_memblock_release(u->memchunk.memblock); + if (pa_memblockq_peek(u->memblockq, chunk) >= 0) { + pa_memblockq_drop(u->memblockq, chunk->length); + return 0; + } - if (n <= 0) - n = 0; + if (!u->sndfile) + break; - u->memchunk.length = n * fs; - } else { - sf_count_t n; - void *p; + tchunk.memblock = pa_memblock_new(i->sink->core->mempool, length); + tchunk.index = 0; - p = pa_memblock_acquire(u->memchunk.memblock); - n = sf_read_raw(u->sndfile, p, length); - pa_memblock_release(u->memchunk.memblock); + if (u->readf_function) { + sf_count_t n; + void *p; + size_t fs = pa_frame_size(&i->sample_spec); - if (n <= 0) - n = 0; + p = pa_memblock_acquire(tchunk.memblock); + n = u->readf_function(u->sndfile, p, length/fs); + pa_memblock_release(tchunk.memblock); - u->memchunk.length = n; - } + if (n <= 0) + n = 0; - if (u->memchunk.length <= 0) { + tchunk.length = n * fs; - pa_memblock_unref(u->memchunk.memblock); - pa_memchunk_reset(&u->memchunk); + } else { + sf_count_t n; + void *p; - pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u), FILE_STREAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL); + p = pa_memblock_acquire(tchunk.memblock); + n = sf_read_raw(u->sndfile, p, length); + pa_memblock_release(tchunk.memblock); - sf_close(u->sndfile); - u->sndfile = NULL; + if (n <= 0) + n = 0; - return -1; - } + tchunk.length = n; } - pa_assert(u->memchunk.memblock); - pa_assert(u->memchunk.length > 0); + if (tchunk.length <= 0) { + + pa_memblock_unref(tchunk.memblock); - if (u->drop < u->memchunk.length) { - u->memchunk.index += u->drop; - u->memchunk.length -= u->drop; - u->drop = 0; + sf_close(u->sndfile); + u->sndfile = NULL; break; } - u->drop -= u->memchunk.length; - pa_memblock_unref(u->memchunk.memblock); - pa_memchunk_reset(&u->memchunk); + pa_memblockq_push(u->memblockq, &tchunk); + pa_memblock_unref(tchunk.memblock); } - *chunk = u->memchunk; - pa_memblock_ref(chunk->memblock); + pa_log_debug("peek fail"); - pa_assert(chunk->length > 0); - pa_assert(u->drop <= 0); + if (pa_sink_input_safe_to_remove(i)) { + pa_log_debug("completed to play"); - return 0; + pa_memblockq_free(u->memblockq); + u->memblockq = NULL; + + pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u), FILE_STREAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL); + } + + return -1; } -static void sink_input_drop_cb(pa_sink_input *i, size_t length) { +static void sink_input_rewind_cb(pa_sink_input *i, size_t nbytes) { file_stream *u; - pa_assert(i); - pa_assert(length > 0); + pa_sink_input_assert_ref(i); + pa_assert(nbytes > 0); u = FILE_STREAM(i->userdata); file_stream_assert_ref(u); - if (u->memchunk.memblock) { + if (!u->memblockq) + return; - if (length < u->memchunk.length) { - u->memchunk.index += length; - u->memchunk.length -= length; - return; - } + pa_memblockq_rewind(u->memblockq, nbytes); +} - length -= u->memchunk.length; - pa_memblock_unref(u->memchunk.memblock); - pa_memchunk_reset(&u->memchunk); - } +static void sink_input_set_max_rewind(pa_sink_input *i, size_t nbytes) { + file_stream *u; - u->drop += length; + pa_sink_input_assert_ref(i); + u = FILE_STREAM(i->userdata); + file_stream_assert_ref(u); + + if (!u->memblockq) + return; + + pa_memblockq_set_maxrewind(u->memblockq, nbytes); } int pa_play_file( @@ -228,6 +235,7 @@ int pa_play_file( pa_sample_spec ss; pa_sink_input_new_data data; int fd; + pa_memblock *silence; pa_assert(sink); pa_assert(fname); @@ -237,10 +245,8 @@ int pa_play_file( u->parent.process_msg = file_stream_process_msg; u->core = sink->core; u->sink_input = NULL; - pa_memchunk_reset(&u->memchunk); u->sndfile = NULL; u->readf_function = NULL; - u->drop = 0; memset(&sfinfo, 0, sizeof(sfinfo)); @@ -312,18 +318,31 @@ int pa_play_file( pa_sink_input_new_data_init(&data); data.sink = sink; data.driver = __FILE__; - data.name = fname; pa_sink_input_new_data_set_sample_spec(&data, &ss); pa_sink_input_new_data_set_volume(&data, volume); + pa_proplist_sets(data.proplist, PA_PROP_MEDIA_NAME, fname); + pa_proplist_sets(data.proplist, PA_PROP_MEDIA_FILENAME, fname); + + u->sink_input = pa_sink_input_new(sink->core, &data, 0); + pa_sink_input_new_data_done(&data); - if (!(u->sink_input = pa_sink_input_new(sink->core, &data, 0))) + if (!u->sink_input) goto fail; - u->sink_input->peek = sink_input_peek_cb; - u->sink_input->drop = sink_input_drop_cb; + u->sink_input->pop = sink_input_pop_cb; + u->sink_input->rewind = sink_input_rewind_cb; + u->sink_input->set_max_rewind = sink_input_set_max_rewind; u->sink_input->kill = sink_input_kill_cb; u->sink_input->userdata = u; + silence = pa_silence_memblock_new( + u->core->mempool, + &u->sink_input->sample_spec, + u->sink_input->thread_info.resampler ? pa_resampler_max_block_size(u->sink_input->thread_info.resampler) : 0); + + u->memblockq = pa_memblockq_new(0, MEMBLOCKQ_MAXLENGTH, 0, pa_frame_size(&u->sink_input->sample_spec), 1, 1, 0, silence); + pa_memblock_unref(silence); + pa_sink_input_put(u->sink_input); /* The reference to u is dangling here, because we want to keep |