2 This file is part of PulseAudio.
4 Copyright 2006-2008 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <pulse/xmalloc.h>
31 #include <pulsecore/sink-input.h>
32 #include <pulsecore/thread-mq.h>
34 #include "play-memblockq.h"
36 typedef struct memblockq_stream
{
39 pa_sink_input
*sink_input
;
40 pa_memblockq
*memblockq
;
44 MEMBLOCKQ_STREAM_MESSAGE_UNLINK
,
47 PA_DEFINE_PRIVATE_CLASS(memblockq_stream
, pa_msgobject
);
48 #define MEMBLOCKQ_STREAM(o) (memblockq_stream_cast(o))
50 static void memblockq_stream_unlink(memblockq_stream
*u
) {
56 pa_sink_input_unlink(u
->sink_input
);
57 pa_sink_input_unref(u
->sink_input
);
60 memblockq_stream_unref(u
);
63 static void memblockq_stream_free(pa_object
*o
) {
64 memblockq_stream
*u
= MEMBLOCKQ_STREAM(o
);
68 pa_memblockq_free(u
->memblockq
);
73 static int memblockq_stream_process_msg(pa_msgobject
*o
, int code
, void*userdata
, int64_t offset
, pa_memchunk
*chunk
) {
74 memblockq_stream
*u
= MEMBLOCKQ_STREAM(o
);
75 memblockq_stream_assert_ref(u
);
78 case MEMBLOCKQ_STREAM_MESSAGE_UNLINK
:
79 memblockq_stream_unlink(u
);
86 static void sink_input_kill_cb(pa_sink_input
*i
) {
89 pa_sink_input_assert_ref(i
);
90 u
= MEMBLOCKQ_STREAM(i
->userdata
);
91 memblockq_stream_assert_ref(u
);
93 memblockq_stream_unlink(u
);
96 /* Called from IO thread context */
97 static void sink_input_state_change_cb(pa_sink_input
*i
, pa_sink_input_state_t state
) {
100 pa_sink_input_assert_ref(i
);
101 u
= MEMBLOCKQ_STREAM(i
->userdata
);
102 memblockq_stream_assert_ref(u
);
104 /* If we are added for the first time, ask for a rewinding so that
105 * we are heard right-away. */
106 if (PA_SINK_INPUT_IS_LINKED(state
) &&
107 i
->thread_info
.state
== PA_SINK_INPUT_INIT
)
108 pa_sink_input_request_rewind(i
, 0, FALSE
, TRUE
, TRUE
);
111 static int sink_input_pop_cb(pa_sink_input
*i
, size_t nbytes
, pa_memchunk
*chunk
) {
114 pa_sink_input_assert_ref(i
);
116 u
= MEMBLOCKQ_STREAM(i
->userdata
);
117 memblockq_stream_assert_ref(u
);
122 if (pa_memblockq_peek(u
->memblockq
, chunk
) < 0) {
124 if (pa_sink_input_safe_to_remove(i
)) {
126 pa_memblockq_free(u
->memblockq
);
129 pa_asyncmsgq_post(pa_thread_mq_get()->outq
, PA_MSGOBJECT(u
), MEMBLOCKQ_STREAM_MESSAGE_UNLINK
, NULL
, 0, NULL
, NULL
);
135 /* If there's no memblock, there's going to be data in the memblockq after
136 * a gap with length chunk->length. Drop the the gap and peek the actual
137 * data. There should always be some data coming - hence the assert. The
138 * gap will occur if the memblockq is rewound beyond index 0.*/
139 if (!chunk
->memblock
) {
140 pa_memblockq_drop(u
->memblockq
, chunk
->length
);
141 pa_assert_se(pa_memblockq_peek(u
->memblockq
, chunk
) >= 0);
144 chunk
->length
= PA_MIN(chunk
->length
, nbytes
);
145 pa_memblockq_drop(u
->memblockq
, chunk
->length
);
150 static void sink_input_process_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
153 pa_sink_input_assert_ref(i
);
154 u
= MEMBLOCKQ_STREAM(i
->userdata
);
155 memblockq_stream_assert_ref(u
);
160 pa_memblockq_rewind(u
->memblockq
, nbytes
);
163 static void sink_input_update_max_rewind_cb(pa_sink_input
*i
, size_t nbytes
) {
166 pa_sink_input_assert_ref(i
);
167 u
= MEMBLOCKQ_STREAM(i
->userdata
);
168 memblockq_stream_assert_ref(u
);
173 pa_memblockq_set_maxrewind(u
->memblockq
, nbytes
);
176 pa_sink_input
* pa_memblockq_sink_input_new(
178 const pa_sample_spec
*ss
,
179 const pa_channel_map
*map
,
183 pa_sink_input_flags_t flags
) {
185 memblockq_stream
*u
= NULL
;
186 pa_sink_input_new_data data
;
191 /* We allow creating this stream with no q set, so that it can be
194 u
= pa_msgobject_new(memblockq_stream
);
195 u
->parent
.parent
.free
= memblockq_stream_free
;
196 u
->parent
.process_msg
= memblockq_stream_process_msg
;
197 u
->core
= sink
->core
;
198 u
->sink_input
= NULL
;
201 pa_sink_input_new_data_init(&data
);
202 pa_sink_input_new_data_set_sink(&data
, sink
, FALSE
);
203 data
.driver
= __FILE__
;
204 pa_sink_input_new_data_set_sample_spec(&data
, ss
);
205 pa_sink_input_new_data_set_channel_map(&data
, map
);
206 pa_sink_input_new_data_set_volume(&data
, volume
);
207 pa_proplist_update(data
.proplist
, PA_UPDATE_REPLACE
, p
);
210 pa_sink_input_new(&u
->sink_input
, sink
->core
, &data
);
211 pa_sink_input_new_data_done(&data
);
216 u
->sink_input
->pop
= sink_input_pop_cb
;
217 u
->sink_input
->process_rewind
= sink_input_process_rewind_cb
;
218 u
->sink_input
->update_max_rewind
= sink_input_update_max_rewind_cb
;
219 u
->sink_input
->kill
= sink_input_kill_cb
;
220 u
->sink_input
->state_change
= sink_input_state_change_cb
;
221 u
->sink_input
->userdata
= u
;
224 pa_memblockq_sink_input_set_queue(u
->sink_input
, q
);
226 /* The reference to u is dangling here, because we want
227 * to keep this stream around until it is fully played. */
229 /* This sink input is not "put" yet, i.e. pa_sink_input_put() has
230 * not been called! */
232 return pa_sink_input_ref(u
->sink_input
);
236 memblockq_stream_unref(u
);
241 int pa_play_memblockq(
243 const pa_sample_spec
*ss
,
244 const pa_channel_map
*map
,
248 pa_sink_input_flags_t flags
,
249 uint32_t *sink_input_index
) {
257 if (!(i
= pa_memblockq_sink_input_new(sink
, ss
, map
, q
, volume
, p
, flags
)))
260 pa_sink_input_put(i
);
262 if (sink_input_index
)
263 *sink_input_index
= i
->index
;
265 pa_sink_input_unref(i
);
270 void pa_memblockq_sink_input_set_queue(pa_sink_input
*i
, pa_memblockq
*q
) {
273 pa_sink_input_assert_ref(i
);
274 u
= MEMBLOCKQ_STREAM(i
->userdata
);
275 memblockq_stream_assert_ref(u
);
278 pa_memblockq_free(u
->memblockq
);
280 if ((u
->memblockq
= q
)) {
281 pa_memblockq_set_prebuf(q
, 0);
282 pa_memblockq_set_silence(q
, NULL
);
283 pa_memblockq_willneed(q
);