30
28
#include <string.h>
32
30
#include <pulse/xmalloc.h>
31
#include <pulse/gccmacro.h>
34
33
#include <pulsecore/sink-input.h>
35
#include <pulsecore/gccmacro.h>
36
34
#include <pulsecore/thread-mq.h>
35
#include <pulsecore/play-memblockq.h>
38
37
#include "play-memchunk.h"
40
typedef struct memchunk_stream {
43
pa_sink_input *sink_input;
48
MEMCHUNK_STREAM_MESSAGE_UNLINK,
51
PA_DECLARE_CLASS(memchunk_stream);
52
#define MEMCHUNK_STREAM(o) (memchunk_stream_cast(o))
53
static PA_DEFINE_CHECK_TYPE(memchunk_stream, pa_msgobject);
55
static void memchunk_stream_unlink(memchunk_stream *u) {
61
pa_sink_input_unlink(u->sink_input);
63
pa_sink_input_unref(u->sink_input);
66
memchunk_stream_unref(u);
69
static void memchunk_stream_free(pa_object *o) {
70
memchunk_stream *u = MEMCHUNK_STREAM(o);
73
memchunk_stream_unlink(u);
75
if (u->memchunk.memblock)
76
pa_memblock_unref(u->memchunk.memblock);
81
static int memchunk_stream_process_msg(pa_msgobject *o, int code, void*userdata, int64_t offset, pa_memchunk *chunk) {
82
memchunk_stream *u = MEMCHUNK_STREAM(o);
83
memchunk_stream_assert_ref(u);
86
case MEMCHUNK_STREAM_MESSAGE_UNLINK:
87
memchunk_stream_unlink(u);
94
static void sink_input_kill_cb(pa_sink_input *i) {
95
pa_sink_input_assert_ref(i);
97
memchunk_stream_unlink(MEMCHUNK_STREAM(i->userdata));
100
static int sink_input_peek_cb(pa_sink_input *i, size_t length, pa_memchunk *chunk) {
105
u = MEMCHUNK_STREAM(i->userdata);
106
memchunk_stream_assert_ref(u);
108
if (!u->memchunk.memblock)
111
if (u->memchunk.length <= 0) {
112
pa_memblock_unref(u->memchunk.memblock);
113
u->memchunk.memblock = NULL;
114
pa_asyncmsgq_post(pa_thread_mq_get()->outq, PA_MSGOBJECT(u), MEMCHUNK_STREAM_MESSAGE_UNLINK, NULL, 0, NULL, NULL);
118
pa_assert(u->memchunk.memblock);
119
*chunk = u->memchunk;
120
pa_memblock_ref(chunk->memblock);
125
static void sink_input_drop_cb(pa_sink_input *i, size_t length) {
129
pa_assert(length > 0);
130
u = MEMCHUNK_STREAM(i->userdata);
131
memchunk_stream_assert_ref(u);
133
if (length < u->memchunk.length) {
134
u->memchunk.length -= length;
135
u->memchunk.index += length;
137
u->memchunk.length = 0;
140
39
int pa_play_memchunk(
143
41
const pa_sample_spec *ss,
144
42
const pa_channel_map *map,
145
43
const pa_memchunk *chunk,
146
pa_cvolume *volume) {
46
uint32_t *sink_input_index) {
148
memchunk_stream *u = NULL;
149
pa_sink_input_new_data data;
155
if (volume && pa_cvolume_is_muted(volume))
158
pa_memchunk_will_need(chunk);
160
u = pa_msgobject_new(memchunk_stream);
161
u->parent.parent.free = memchunk_stream_free;
162
u->parent.process_msg = memchunk_stream_process_msg;
163
u->core = sink->core;
164
u->memchunk = *chunk;
165
pa_memblock_ref(u->memchunk.memblock);
167
pa_sink_input_new_data_init(&data);
169
data.driver = __FILE__;
171
pa_sink_input_new_data_set_sample_spec(&data, ss);
172
pa_sink_input_new_data_set_channel_map(&data, map);
173
pa_sink_input_new_data_set_volume(&data, volume);
175
if (!(u->sink_input = pa_sink_input_new(sink->core, &data, 0)))
178
u->sink_input->peek = sink_input_peek_cb;
179
u->sink_input->drop = sink_input_drop_cb;
180
u->sink_input->kill = sink_input_kill_cb;
181
u->sink_input->userdata = u;
183
pa_sink_input_put(u->sink_input);
185
/* The reference to u is dangling here, because we want to keep
186
* this stream around until it is fully played. */
55
q = pa_memblockq_new(0, chunk->length, 0, pa_frame_size(ss), 1, 1, 0, NULL);
56
pa_assert_se(pa_memblockq_push(q, chunk) >= 0);
58
if ((r = pa_play_memblockq(sink, ss, map, q, volume, p, sink_input_index)) < 0) {
192
memchunk_stream_unref(u);