2004-08-17 19:47:42 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2004-2006 Lennart Poettering
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2004-08-17 19:47:42 +00:00
|
|
|
by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-08-17 19:47:42 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2004-08-17 19:47:42 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-08-04 16:42:37 +00:00
|
|
|
#include <stdlib.h>
|
2004-08-27 01:29:49 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
2004-08-04 16:42:37 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
2006-05-17 16:34:18 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/sink-input.h>
|
|
|
|
|
#include <pulsecore/gccmacro.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/thread-mq.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2004-08-04 16:42:37 +00:00
|
|
|
#include "play-memchunk.h"
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
typedef struct memchunk_stream {
|
|
|
|
|
pa_msgobject parent;
|
|
|
|
|
pa_core *core;
|
|
|
|
|
pa_sink_input *sink_input;
|
|
|
|
|
pa_memchunk memchunk;
|
|
|
|
|
} memchunk_stream;
|
2004-08-04 16:42:37 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
enum {
|
|
|
|
|
MEMCHUNK_STREAM_MESSAGE_UNLINK,
|
|
|
|
|
};
|
2004-09-14 20:53:25 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
PA_DECLARE_CLASS(memchunk_stream);
|
|
|
|
|
#define MEMCHUNK_STREAM(o) (memchunk_stream_cast(o))
|
|
|
|
|
static PA_DEFINE_CHECK_TYPE(memchunk_stream, pa_msgobject);
|
|
|
|
|
|
|
|
|
|
static void memchunk_stream_unlink(memchunk_stream *u) {
|
|
|
|
|
pa_assert(u);
|
|
|
|
|
|
|
|
|
|
if (!u->sink_input)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
pa_sink_input_unlink(u->sink_input);
|
|
|
|
|
|
|
|
|
|
pa_sink_input_unref(u->sink_input);
|
|
|
|
|
u->sink_input = NULL;
|
|
|
|
|
|
|
|
|
|
memchunk_stream_unref(u);
|
2004-08-04 16:42:37 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void memchunk_stream_free(pa_object *o) {
|
|
|
|
|
memchunk_stream *u = MEMCHUNK_STREAM(o);
|
|
|
|
|
pa_assert(u);
|
2004-08-04 16:42:37 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
memchunk_stream_unlink(u);
|
|
|
|
|
|
|
|
|
|
if (u->memchunk.memblock)
|
|
|
|
|
pa_memblock_unref(u->memchunk.memblock);
|
|
|
|
|
|
|
|
|
|
pa_xfree(u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int memchunk_stream_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
|
|
|
|
|
memchunk_stream *u = MEMCHUNK_STREAM(o);
|
|
|
|
|
memchunk_stream_assert_ref(u);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
switch (code) {
|
|
|
|
|
case MEMCHUNK_STREAM_MESSAGE_UNLINK:
|
|
|
|
|
memchunk_stream_unlink(u);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2004-08-04 16:42:37 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void sink_input_kill_cb(pa_sink_input *i) {
|
|
|
|
|
pa_sink_input_assert_ref(i);
|
|
|
|
|
|
|
|
|
|
memchunk_stream_unlink(MEMCHUNK_STREAM(i->userdata));
|
2004-08-04 16:42:37 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static int sink_input_peek_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
|
|
|
|
|
memchunk_stream *u;
|
|
|
|
|
|
|
|
|
|
pa_assert(i);
|
|
|
|
|
pa_assert(chunk);
|
|
|
|
|
u = MEMCHUNK_STREAM(i->userdata);
|
|
|
|
|
memchunk_stream_assert_ref(u);
|
2004-08-04 16:42:37 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (!u->memchunk.memblock)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (u->memchunk.length <= 0) {
|
|
|
|
|
pa_memblock_unref(u->memchunk.memblock);
|
|
|
|
|
u->memchunk.memblock = NULL;
|
|
|
|
|
pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u), MEMCHUNK_STREAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2004-08-04 16:42:37 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(u->memchunk.memblock);
|
|
|
|
|
*chunk = u->memchunk;
|
|
|
|
|
pa_memblock_ref(chunk->memblock);
|
2004-08-04 16:42:37 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void sink_input_drop_cb(pa_sink_input *i, size_t length) {
|
|
|
|
|
memchunk_stream *u;
|
|
|
|
|
|
|
|
|
|
pa_assert(i);
|
|
|
|
|
pa_assert(length > 0);
|
|
|
|
|
u = MEMCHUNK_STREAM(i->userdata);
|
|
|
|
|
memchunk_stream_assert_ref(u);
|
|
|
|
|
|
|
|
|
|
if (length < u->memchunk.length) {
|
|
|
|
|
u->memchunk.length -= length;
|
|
|
|
|
u->memchunk.index += length;
|
|
|
|
|
} else
|
|
|
|
|
u->memchunk.length = 0;
|
2004-08-04 16:42:37 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
int pa_play_memchunk(
|
2006-08-13 16:19:56 +00:00
|
|
|
pa_sink *sink,
|
|
|
|
|
const char *name,
|
|
|
|
|
const pa_sample_spec *ss,
|
|
|
|
|
const pa_channel_map *map,
|
|
|
|
|
const pa_memchunk *chunk,
|
|
|
|
|
pa_cvolume *volume) {
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
memchunk_stream *u = NULL;
|
2006-08-13 16:19:56 +00:00
|
|
|
pa_sink_input_new_data data;
|
2004-08-04 16:42:37 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(sink);
|
|
|
|
|
pa_assert(ss);
|
|
|
|
|
pa_assert(chunk);
|
2004-08-04 16:42:37 +00:00
|
|
|
|
2006-08-13 16:19:56 +00:00
|
|
|
if (volume && pa_cvolume_is_muted(volume))
|
2004-08-04 16:42:37 +00:00
|
|
|
return 0;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_memchunk_will_need(chunk);
|
|
|
|
|
|
|
|
|
|
u = pa_msgobject_new(memchunk_stream);
|
|
|
|
|
u->parent.parent.free = memchunk_stream_free;
|
|
|
|
|
u->parent.process_msg = memchunk_stream_process_msg;
|
|
|
|
|
u->core = sink->core;
|
|
|
|
|
u->memchunk = *chunk;
|
|
|
|
|
pa_memblock_ref(u->memchunk.memblock);
|
|
|
|
|
|
2006-08-13 16:19:56 +00:00
|
|
|
pa_sink_input_new_data_init(&data);
|
|
|
|
|
data.sink = sink;
|
|
|
|
|
data.driver = __FILE__;
|
2007-10-28 19:13:50 +00:00
|
|
|
data.name = name;
|
2006-08-13 16:19:56 +00:00
|
|
|
pa_sink_input_new_data_set_sample_spec(&data, ss);
|
|
|
|
|
pa_sink_input_new_data_set_channel_map(&data, map);
|
|
|
|
|
pa_sink_input_new_data_set_volume(&data, volume);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (!(u->sink_input = pa_sink_input_new(sink->core, &data, 0)))
|
|
|
|
|
goto fail;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
u->sink_input->peek = sink_input_peek_cb;
|
|
|
|
|
u->sink_input->drop = sink_input_drop_cb;
|
|
|
|
|
u->sink_input->kill = sink_input_kill_cb;
|
|
|
|
|
u->sink_input->userdata = u;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_sink_input_put(u->sink_input);
|
2004-08-04 16:42:37 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
/* The reference to u is dangling here, because we want to keep
|
|
|
|
|
* this stream around until it is fully played. */
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-08-04 16:42:37 +00:00
|
|
|
return 0;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
if (u)
|
|
|
|
|
memchunk_stream_unref(u);
|
|
|
|
|
|
|
|
|
|
return -1;
|
2004-08-04 16:42:37 +00:00
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
|